Python Forum
hangman doesn't end after guessing the word
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
hangman doesn't end after guessing the word
#1
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!
Reply
#2
Which part of the code doesnt work? It seems to run normal for me.
Reply
#3
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.
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Hangman Troubleshooting Ewilliam51 5 3,057 Jul-22-2020, 07:09 AM
Last Post: Sooqa
  fun with hangman! Low_Ki_ 7 6,919 Jun-13-2017, 05:03 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020