![]() |
hangman doesn't end after guessing the word - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: hangman doesn't end after guessing the word (/thread-22294.html) |
hangman doesn't end after guessing the word - vinci - Nov-06-2019 Hi, I should say from the beginning that I'm very new in Python. I've been following a video tutorial where hangman is being tested. I've got the following code which seems to work for the guy who teaches, but not to me. And I simply don't understand the logic of it to be honest and can't understand how it can actually work. This is the code: import random # make a list of words words = [ 'apple', 'banana', 'orange', 'coconut', 'strawberry', 'lime', 'grapefruit', 'lemon', 'kumquat', 'blueberry', 'melon' ] while True: start = input("Press enter/return to start, or enter Q to quit") if start.lower() == 'q': break # pick a random word # choice functions picks a random item out of an iterable secret_word = random.choice(words) print("secret_word is {}".format(secret_word)) bad_guesses = [] good_guesses = [] while len(bad_guesses) < 7 and len(good_guesses) != len(list(secret_word)): # draw spaces # draw guessed letters and strikes for letter in secret_word: if letter in good_guesses: print(letter, end='') else: print('_', end='') print('') print('Strikes: {}/7'.format(len(bad_guesses))) print('') # take a guess guess = input("Guess a letter: ").lower() if len(guess) != 1: print("You can only guess a single letter!") continue elif guess in bad_guesses or guess in good_guesses: print("You've already guessed that letter!") continue elif not guess.isalpha(): print("You can only guess letters!") continue if guess in secret_word: good_guesses.append(guess) if len(good_guesses) == len(list(secret_word)): print("You win! The word was {}".format(secret_word)) break else: bad_guesses.append(guess) else: print("You didn't guess it! My secret word was {}".format(secret_word))What I don't understand is why how this line "if len(good_guesses) == len(list(secret_word)" can actually work. The only situation where I'd assume it can work is when you've only guessed words whose letters don't repeat. So for a word like 'blueberry' it won't work. Is this a mistake that the guy has made? How could I improve the code, so that the game ends also when I have to guess words whose letters repeat. Thanks! RE: hangman doesn't end after guessing the word - Cryptus - Nov-06-2019 Which part of the code doesnt work? It seems to run normal for me. RE: hangman doesn't end after guessing the word - vinci - Nov-06-2019 The code works. The problem is that it doesn't work as it's supposed to work when the word contains letters which repeat themselves, as I've already stated. I've just found the solution, though. Instead of using list the conditional, I should use set, and then a word like 'blueberry' works fine: if len(good_guesses) == len(set(secret_word)):P.S. to be more explicit, if a word like 'blueberry' is chosen, then the program will never exit, you keep guessing and you're going to be kept in a loop. RE: hangman doesn't end after guessing the word - ichabod801 - Nov-06-2019 You're right, the game does not end if there is a double letter in the word. And you can't guess the same letter twice. It shouldn't be testing against len(list(secret_word)), but rather against len(set(secret_word)). Using set(secret_word) will give you all the unique letters in the string. |