In my code below, when I input the correct answer, the ‘wrong…guess again’ keeps printing. This is within the first if statement. I dont know how to set up a conditional for it to print only when the answer is wrong. thanks for the help.
out_of_guesses = False answer = 'dog' max_guess = 3 guess = '' guess_counter = 0 while guess != answer and not(out_of_guesses): if guess_counter < max_guess: guess = input('pick an animal') guess_counter += 1 print ('wrong...guess again') else: out_of_guesses = True if out_of_guesses: print ('you lose') else: print('you win')
I typed up everything on the first paragraph.
It looks like the issue is with the indentation of the print('wrong...guess again') statement. It is currently inside the if guess_counter < max_guess: block, so it will be executed every time, regardless of whether the guess is correct or not.
print('wrong...guess again')
if guess_counter < max_guess:
You should move the print statement outside of the if block, like this:
print
if
out_of_guesses = False answer = 'dog' max_guess = 3 guess = '' guess_counter = 0 while guess != answer and not(out_of_guesses): if guess_counter < max_guess: guess = input('pick an animal') guess_counter += 1 if guess != answer: # Check if the guess is wrong print('wrong...guess again') else: out_of_guesses = True if out_of_guesses: print('you lose') else: print('you win')
Now, the print('wrong...guess again') statement is inside a nested if block that checks whether the guess is wrong or not. It will only print when the guess is incorrect, and it won’t print when the guess is correct.