3
\$\begingroup\$

I'm super excited to share my hangman game with you. (haven't added the graphics yet...)

I will be using this program as my final project (for school), but I would like to have this polished before handing it in. I have given my program the limit of 7 attempts, but if the user continues to guess the wrong letters and then suddenly guesses the right letter, the attempt count stays where it's at.

Here's what I mean:

Let's say the word the computer has chosen is 'cougar' (I'm going to pretend I don't actually know this).

BTW this is on my console:

Guess the word: ______
7 chances left

a # <-- my input (1st actual input)
Yes! a is in the word!

Guess the word: ____a_
7 chances left

b # <-- my input (2nd actual input)
Sorry, b is not in the word. Try again.

Guess the word: ____a_
6 chances left

b # <-- my input (this was just to test out if my program would let me know that I have repeated a letter)
You already guessed b. Please try another one!

Guess the word: ____a_
6 chances left

t # <-- my input (3rd actual input)
Sorry, t is not in the word. Try again.

Guess the word: ____a_
5 chances left

q # <-- my input (4th actual input)
Sorry, q is not in the word. Try again.

Guess the word: ____a_
4 chances left

r # <-- my input (5th actual input)
Yes! r is in the word!

Guess the word: ____ar
4 chances left

h # <-- my input (6th actual input)
Sorry, h is not in the word. Try again.

Guess the word: ____ar
3 chances left

c # <-- my input (7th actual input)

this is where the program should've initially said 'Sorry, you ran out of attempts. The word was cougar. Maybe next time ☹.

instead, I get this:

Yes! c is in the word!

Guess the word: c___ar
3 chances left # <-- this is WRONG!!

g # <-- my input (my 1st extra attempt)
Yes! g is in the word!

Guess the word: c__gar
3 chances left

i # <-- my input (my 2nd extra attempt)
Sorry, i is not in the word. Try again.

Guess the word: c__gar
2 chances left

o # <-- my input (my 3rd extra attempt)
Yes! o is in the word!

Guess the word: co_gar
2 chances left

u # <-- my input (my 4th extra attempt)
Yes! u is in the word!

Awesome! You guessed cougar!

So my program is supposed to allow only 7 attempts (with the exception of a repeated letter). As you can clearly see I got 12 attempts! Out of which only 8 were acceptable (7 actual attempts and 1 for repeating 'b'), meaning I got 4 EXTRA attempts. (this is what I need to fix).

Here's my code:

from random import choice

words = choice(['ant', 'alpaca', 'baboon', 'badger', 'bat', 'bear', 'beaver', 'camel', 'cat', 'clam', 'cobra', 'cougar',
                'coyote', 'crow', 'deer', 'dog', 'donkey', 'duck', 'eagle', 'ferret', 'fox', 'frog', 'goat', 'goose',
                'hawk', 'lion', 'lizard', 'llama', 'mole', 'monkey', 'moose', 'mouse', 'mule', 'newt', 'otter', 'owl',
                'panda', 'parrot', 'pigeon', 'python', 'rabbit', 'ram', 'rat', 'raven', 'rhino', 'salmon', 'seal',
                'shark', 'sheep', 'skunk', 'sloth', 'snake', 'spider', 'stork', 'swan', 'tiger', 'toad', 'trout',
                'turkey', 'turtle', 'weasel', 'whale', 'wolf', 'wombat', 'zebra'])

guessed = []
wrong = []
attempts = 7

while attempts > 0:

    out = ''

    for letter in words:
        if letter in guessed:
            out = out + letter
        else:
            out = out + '_'

    if out == words:
        break

    print('Guess the word:',out)
    print(attempts,'chances left\n')

    guess = input()

    if guess in guessed or guess in wrong:
        print('You already guessed',guess+ '.','Please try another one!')
    elif guess in words:
        print('Yes!',guess,'is in the word!')
        guessed.append(guess)
    else:
        print('Sorry,',guess,'is not in the word. Try again.')
        attempts = attempts - 1
        wrong.append(guess)

    print()

if attempts:
    print('Awesome! You guessed',words + '!')
else:
    print('Sorry you ran out of attempts. The word was',words + '.', 'Maybe next time ☹')
\$\endgroup\$
2
  • \$\begingroup\$ All you need to do to fix your code is add attempts = attempts - 1 when the person guesses right. But normally when playing hangman correct guesses do not count. Are you sure you want your game to work like that? \$\endgroup\$ Commented Dec 4, 2020 at 19:14
  • \$\begingroup\$ @ThisIsAQuestion I think that would be better as it just leads me to get confused... \$\endgroup\$
    – seoul_007
    Commented Dec 4, 2020 at 20:13

2 Answers 2

2
\$\begingroup\$

Great job on your project! You're amazing!

I reviewed your code and, rather than fixing the attempts variable, I removed it entirely and used 7-len(wrong) to get the remaining attempts. This seems to fix your issue. Also, I change the if statement at the end to if len(wrong) != 7 because the other if statement didn't make sense while using len. The way I put it, it will only congratulate you if you have less than 7 attempts used before it calls the break.

The hardest part of fixing the code was testing it as I suck at hangman ;). If you need more help with this project, please let me know!

Here is your code:

from random import choice

words = choice(['ant', 'alpaca', 'baboon', 'badger', 'bat', 'bear', 'beaver', 'camel', 'cat', 'clam', 'cobra', 'cougar',
                'coyote', 'crow', 'deer', 'dog', 'donkey', 'duck', 'eagle', 'ferret', 'fox', 'frog', 'goat', 'goose',
                'hawk', 'lion', 'lizard', 'llama', 'mole', 'monkey', 'moose', 'mouse', 'mule', 'newt', 'otter', 'owl',
                'panda', 'parrot', 'pigeon', 'python', 'rabbit', 'ram', 'rat', 'raven', 'rhino', 'salmon', 'seal',
                'shark', 'sheep', 'skunk', 'sloth', 'snake', 'spider', 'stork', 'swan', 'tiger', 'toad', 'trout',
                'turkey', 'turtle', 'weasel', 'whale', 'wolf', 'wombat', 'zebra'])

guessed = []
wrong = []

while len(wrong) < 7:

    out = ''

    for letter in words:
        if letter in guessed:
            out = out + letter
        else:
            out = out + '_'

    if out == words:
        break

    print('Guess the word:',out)
    print(7-len(wrong),'chances left\n')

    guess = input()

    if guess in guessed or guess in wrong:
        print('You already guessed',guess+ '.','Please try another one!')
    elif guess in words:
        print('Yes!',guess,'is in the word!')
        guessed.append(guess)
    else:
        print('Sorry,',guess,'is not in the word. Try again.')
        wrong.append(guess)

    print()

if len(wrong) != 7:
    print('Awesome! You guessed',words + '!')
else:
    print('Sorry you ran out of attempts. The word was',words + '.', 'Maybe next time ☹')
\$\endgroup\$
2
  • \$\begingroup\$ Thank you so much!! And @Salocor YOU'RE awesome for taking the time to help me!! \$\endgroup\$
    – seoul_007
    Commented Dec 4, 2020 at 19:37
  • \$\begingroup\$ No problem! If this solved your problem, it'd be great if you marked my solution as the answer :). \$\endgroup\$
    – Salocor
    Commented Dec 4, 2020 at 19:45
4
\$\begingroup\$

You are improving, that's nice.

Since the project is quite minimal, I would just highlight few points for consideration.

Misleading variable names

words and out are misleading, what does out mean, your variables should explain themselves clearly and be as unambiguous as possible, words gives the intent of holding multiple words but actually, it holds just a word.

You might not need it

wrong list is totally unnecessary here, you might have implemented it for future purposes but right now, it isn't really used, define only what you are currently using. So it means guess in wrong is an irrelevant check

guessed happens to append only correct guesses, that's misleading, you might want to make it append both correct and wrong guess and it makes the variable wrong even more irrelevant.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you for your insight @theProgrammer. I appreciate your feedback and I will make sure to use your suggestions :) \$\endgroup\$
    – seoul_007
    Commented Dec 4, 2020 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.