Skip to content

Instantly share code, notes, and snippets.

@rikoudosenin rikoudosenin/hangman.py Secret
Created May 29, 2017

Embed
What would you like to do?
import random
alphabeth = 'abcdefghijklmnopqrstuvwxyz'
rand = ''
blank = []
rand_list = []
guessed_list = []
class hangman:
def prepWord(self):
global rand, guessed_list, blank, rand_list
words = ['note', 'pencil', 'paper','foo']
rand = random.choice(words)
guessed_list = []
blank = ['_']*len(rand)
rand_list = []
reaction=['good job','lucky guess!', 'you\'re on a roll']
self.react = random.choice(reaction)
for letter in rand:
rand_list.append(letter)
game.startPlay()
def startPlay(self):
gameQ = input('Play Hangman? y or n --> ')
if gameQ == 'y':
print('Guess the letters:')
print(blank)
game.checkAnswer()
elif gameQ == 'n':
print('goodbye')
def playAgain(self):
again = input('Would you like to play again? y or n --> ')
if again == 'y':
game.prepWord()
else:
Print ('Thanks for playing!')
def checkAnswer(self):
x = True
while x:
answer = input('').lower()
if answer not in guessed_list:
guessed_list.append(answer)
if len(answer)>1:
print('One letter at a time.')
elif answer not in alphabeth:
print('Invalid character, please try again.')
else:
if answer in rand:
print ("The letter {} is in the word. Good guess!".format(answer))
indices = [ndex for ndex, letter in enumerate(rand_list) if letter == answer]
for ndex in indices:
blank[ndex] = answer
print (blank)
else:
print ("I'm sorry the letter {} is not in the word. Please try again.".format(answer))
else:
print('Letter {} already used. Try another.'.format(answer))
if '_' not in blank:
print(self.react)
final_word = ''
for letter in blank:
final_word += letter
print(final_word)
print('')
x = False
game.playAgain()
game = hangman()
game.prepWord()
@rikoudosenin

This comment has been minimized.

Copy link
Owner Author

commented May 29, 2017

You can use while True: instead of assigning true to a variable then using it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.