Skip to content

Hangman game now can be start over after one round #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 27, 2021
Merged
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
86 changes: 51 additions & 35 deletions Hangman/hangman.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,71 @@
from hangman_art import logo , stages
from hangman_art import logo, stages
from hangman_words import wordlist
import random

# get random word from hangman_words
random_word = random.choice(wordlist)
word_len = len(random_word)
user_quit = False

print(logo)
print('This is your word.Try to guess it!!')
print('Word : ',end =" ")

guess_word = ['_'] * word_len
print(*guess_word)
def is_invalid_input(let):
return let.isdigit() or len(let) != 1

chances = 6
end_of_game = False

# game logic
while not end_of_game and chances != 0:
while not user_quit:
print(logo)
print('This is your word.Try to guess it!!')
print('Word : ', end=" ")

flag = 0
letter = input('\n\nGuess a letter: ').lower()
guess_word = ['_'] * word_len
print(*guess_word)

if (letter.isdigit() or len(letter) != 1):
print('Please enter single letter (Numbers not allowed)')
continue
chances = 6
end_of_game = False

if letter in guess_word:
print(f"You've already guessed {letter}")
# game logic
while not end_of_game and chances != 0:
flag = 0
letter = input('\n\nGuess a letter: ').lower()

for i in range(word_len):
if random_word[i] == letter:
guess_word[i] = letter
flag = 1
if is_invalid_input(letter):
print('Please enter single letter (Numbers not allowed)')
continue

if '_' not in guess_word:
end_of_game = True
print('\nCongo!! You won. :)')
break
if letter in guess_word:
print(f"You've already guessed {letter}")

if flag == 0:
print(f"You guessed {letter}, that's not in the word. You lose a life")
chances -= 1

print('Guessed Word : ',end =" ")
print(*guess_word)
print(stages[6-chances])

else:
print('\nYou lost :(\nCorrect word:',random_word)
for i in range(word_len):
if random_word[i] == letter:
guess_word[i] = letter
flag = 1

if '_' not in guess_word:
end_of_game = True
print('\nCongo!! You won. :)')
break

if flag == 0:
print(f"You guessed {letter}, that's not in the word. You lose a life")
chances -= 1

print('Guessed Word : ', end=" ")
print(*guess_word)
print(stages[6 - chances])

else:
print('\nYou lost :(\nCorrect word:', random_word)

while not user_quit:
user_input = input('Do you want to play again? (y/n)')
if not is_invalid_input(user_input):
user_input = user_input.strip().lower()
if user_input=='y' :
break
elif user_input == 'n':
user_quit =True
print('Goodbye!')
else:
print('wrong input, please enter y or n')
else:
print('wrong input, please enter y or n')