diff --git a/Hangman/hangman.py b/Hangman/hangman.py index ac6c8a68..791315da 100644 --- a/Hangman/hangman.py +++ b/Hangman/hangman.py @@ -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') \ No newline at end of file