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 ☹')