Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 26 additions & 33 deletions hangman.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,61 +12,54 @@

import random
from words import words
from hangman_visual import lives_visual_dict
import string


def get_valid_word(words):
word = random.choice(words) # randomly chooses something from the list
while '-' in word or ' ' in word:
word = random.choice(words)
while '-' in word or ' ' in word: #still process code if we find - or space sign
word = random.choice(words)

return word.upper()


def hangman():
word = get_valid_word(words)
word_letters = set(word) # letters in the word
word_letters = set(word)
alphabet = set(string.ascii_uppercase)
used_letters = set() # what the user has guessed
used_letters = set()

lives = 7
lives = 6

# getting user input
while len(word_letters) > 0 and lives > 0:
# letters used
# ' '.join(['a', 'b', 'cd']) --> 'a b cd'
print('You have', lives, 'lives left and you have used these letters: ', ' '.join(used_letters))
while len(word_letters) > 0 and lives > 0:

# what current word is (ie W - R D)
print('You have', lives, 'lives left and You have used that letters: ', ' '.join(used_letters))
word_list = [letter if letter in used_letters else '-' for letter in word]
print(lives_visual_dict[lives])
print('Current word: ', ' '.join(word_list))

user_letter = input('Guess a letter: ').upper()
if user_letter in alphabet - used_letters:
used_letters.add(user_letter)
if user_letter in word_letters:
word_letters.remove(user_letter)
print('')

else:
lives = lives - 1 # takes away a life if wrong
print('\nYour letter,', user_letter, 'is not in the word.')

lives = lives - 1
print('Letter is not in word')
elif user_letter in used_letters:
print('\nYou have already used that letter. Guess another letter.')

print('You have already used that character. Please try again. ')
else:
print('\nThat is not a valid letter.')
print('Invalid character. Please try again')

# gets here when len(word_letters) == 0 OR when lives == 0
if lives == 0:
print(lives_visual_dict[lives])
print('You died, sorry. The word was', word)
if lives == 0 :
print('You Died. The word was ', word)
else:
print('YAY! You guessed the word', word, '!!')


if __name__ == '__main__':
hangman()
print('You guessed the word right', word, ' !!')


hangman()
#made replay again choice
while True:
play = input('Still wanna replay again ? (y/n) ')
if play == 'y' or play == 'Y':
hangman()
else:
quit()