Bio :
Coding Languages :
My sample script is a simple hangman game. It opens up in command line and can be played in command line. The game requires a dictionary labeled american to be in the same folder.
# This script runs a simple hangman game in command line.
#
# Last edit: 2/10/20
#required modules
import random
import argparse
class Hangman(object):
# Opens up the dictionary
def __init__(self, level = 5, non_ascii = False, dictionary = 'american'):
self.file_object = open(dictionary, 'r').read().split()
self.non_ascii = non_ascii
self.level = level
return
def play(self):
# Letter that can be present in a word
= ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', "'"]
normal_letters = 0
normal_checker # Chooses an eligible word from the dictionary
= random.choice(self.file_object).lower()
first_word if len(first_word) < self.level:
= random.choice(self.file_object).lower()
first_word if self.non_ascii == False:
while normal_checker == 0:
for i in first_word:
if i not in normal_letters:
= random.choice(self.file_object).lower()
first_word if len(first_word) < self.level:
= random.choice(self.file_object).lower()
first_word elif (i in normal_letters) and (i is first_word[len(first_word) - 1]):
= 1
normal_checker else:
pass
# Removes apostrophe(s) from the word
if "'" in first_word:
= list(first_word)
first_word "'")
first_word.remove(= ''.join(first_word)
first_word print('Word contains an apostrophe. Apostrophe has been removed.')
= ' '.join(first_word)
word = ' ' * len(word)
x = 0
incorrect_count = 0
g = ''
letters_guessed
# Multiple board states for different stages of the game
= [
boards '-----\n| |\n' + '|\n' * 6,
'-----\n| |\n' + '| O\n' + '|\n' * 5,
'-----\n| |\n' + '| O\n' + '| |\n' + '|\n' * 4,
'-----\n| |\n' + '| O\n' + '| /|\n' + '|\n' * 4,
'-----\n| |\n' + '| O\n' + '| /|\\\n' + '|\n' * 4,
'-----\n| |\n' + '| O\n' + '| /|\\\n' '| /\n' + '|\n' * 3,
'-----\n| |\n' + '| O\n' + '| /|\\\n' '| / \\\n' + '|\n' * 3
]
print(boards[0] + '\n' + '- ' * len(first_word) + '\n' + 'Guessed Letters:')
# Handles guessing procedure for the game
while (x not in word) and (g == 0):
= 0
same_letter = 0
long_letter = 0
duplicate_guess = input('Guess a letter: ')
letter if (letter in x) or (letter in letters_guessed):
= 1
duplicate_guess print('\n')
if duplicate_guess == 0:
for i in range(0, len(word)):
if letter is word[i]:
= list(x)
x = letter
x[i] = ''.join(x)
x if same_letter == 0:
print('Correct!')
= 1
same_letter elif len(letter) > 1:
= 1
long_letter elif i == len(word) - 1 and same_letter == 0:
+= 1
incorrect_count if incorrect_count == 6:
print('Incorrect.')
= 1
g else:
print('Incorrect. Guess again.')
+= ' ' + letter
letters_guessed else:
pass
# Handles error if more than one character is guessed
if long_letter == 1:
print(boards[incorrect_count] + x + '\n' + '- ' * len(first_word) + '\n' + 'Guessed Letters: ' + letters_guessed + '\n' + '\nPlease only enter one letter.')
# Handles error if a letter that has been guessed is guessed again
elif duplicate_guess == 1:
print(boards[incorrect_count] + x + '\n' + '- ' * len(first_word) + '\n' + 'Guessed Letters: ' + letters_guessed + '\n' + '\nYou already guessed this letter.')
elif (x not in word) and (g == 0) and (duplicate_guess == 0):
print(boards[incorrect_count] + x + '\n' + '- ' * len(first_word) + '\n' + 'Guessed Letters: ' + letters_guessed)
if g == 1:
print(boards[incorrect_count] + x + '\n' + '- ' * len(first_word) + '\n' + 'Guessed Letters: ' + letters_guessed + '\n')
print('\nYou lose! The word was ' + first_word + '.\n')
if x in word:
print(boards[incorrect_count] + x + '\n' + '- ' * len(first_word) + '\n' + 'Guessed Letters: ' + letters_guessed + '\n')
print('You win!\n')
= input('Play again?(Y/N) ')
play_again if (play_again == 'Y') or (play_again == 'y'):
self.play()
else:
exit()
if __name__ == "__main__":
= argparse.ArgumentParser(description='Play hangman. All letters are lowercase and there are no symbols.')
parser = parser.parse_args()
args from hangman import *
= Hangman()
h h.play()