0

enter image description hereI'm new to Python but I can't seem to figure out why the declared "attempt" variable is not decremented if an incorrect attempt is submitted.

My thoughts are once a guess is submitted if it's not equal to the correctAnswer then the variable attempts should be decremeted by 1 so the print statement would say "You have 2 attempts left.

# You have 3 attempts to guess the correct word.  If you don't you lose!

attempts = 3
correctAnswer = 32
while attempts != 0:
    guess = input("How old is Dad?") 
    if guess != correctAnswer:
        attempts = attempts - 1 #This should decrement if the attempt is incorrect
        print("You have " + str(attempts) + " left")
    else:
        print("You lose!")

print("You are right!")
  • 2
    Try to change `int(input())`. And if you like to debug the code - try `pythontutor.com` – Daniel Hao Jan 18 '22 at 03:08
  • 1
    The variable obviously does get updated, your own prints show it. – Kelly Bundy Jan 18 '22 at 03:32
  • I'd expect the print statement to update the value for "attempt" so that it would equal 2 if an incorrect guess is submitted. It doesn't it just prints 3 over and over. – Casey Van Dyke Jan 18 '22 at 03:36
  • That's simply not true. – Kelly Bundy Jan 18 '22 at 03:37
  • Someone familiar with that IDE might directly see what happened, but I'm not. Most likely you either forgot to save the code after the latest changes (so you're still running an old version) or saved it in a different file (and are still running the old file). What happens if you change the text `You have` to `You got`, save the code and run again? Likely you'll either both see your variable decrease and the changed text, or you'll see neither. That then points to which situation you're in. – Kelly Bundy Jan 19 '22 at 21:14

1 Answers1

-1

The variable is being updated but you have a problem with the input comparison and your print logic is kinda backwards, making the output look strange. The input function returns strings, and strings are never equal to integers. You need to convert the string to an integer. That will fail if you are given bad input so you can either check the value before you attempt a conversion or use a try/except block to catch errors.

You could pull the final success/failure condition out of the loop:

attempts = 3
correctAnswer = 32
while attempts != 0:
    guess_str = input("How old is Dad?")
    if not guess_str.isdigit():
        attempts = attempts - 1
        print("Please enter a number, yYou have " + str(attempts) + " left")
        continue
    guess = int(guess_str)
    if guess != correctAnswer:
        attempts = attempts - 1 #This should decrement if the attempt is incorrect
        print("You have " + str(attempts) + " left")
    else:
        break
 
if attempts:
    print("You are right!")
else:
    print("You lose!")

But python while loops have an else clause that only runs if the while is not terminated with a break. Add a break to the success path and it will catch the fail path.

attempts = 3
correctAnswer = 32
while attempts != 0:
    guess_str = input("How old is Dad?")
    if not guess_str.isdigit():
        attempts = attempts - 1
        print("Please enter a number, yYou have " + str(attempts) + " left")
        continue
    guess = int(guess_str)
    if guess != correctAnswer:
        attempts = attempts - 1
        print("You have " + str(attempts) + " left")
    else:
        print("You are right!")
        break
else: 
    print("You lose!")
tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • Doesn't answer what the question is all about, so after reading this they still don't know why the variable **doesn't** get updated for them, as shown by their screenshot. This was somewhat reasonable (albeit distracting) as a comment, but isn't an answer. – Kelly Bundy Jan 19 '22 at 20:10
  • @KellyBundy - There was a data conversion and logic problem with the posted code, which I addressed. OP may have been somewhat confused by the program output - perhaps a different version of the code was posted than what he ran locally, I don't know. I ran off of the posted code and saw a different output and several bugs that needed to be fixed, and that's what I based comments and answer on. – tdelaney Jan 19 '22 at 20:42
  • Question title "Global variable not updated..." start of my answer "The variable is being updated...", yeah, I answer the quesiton. – tdelaney Jan 19 '22 at 20:51
  • The data conversion and logic problem are not what the question is about. If you can't reproduce what the question is about, there's a close-vote reason for exactly that. – Kelly Bundy Jan 19 '22 at 20:51
  • That doesn't *answer* it but merely *addresses* it, and the *proper* way to address it is to close-vote accordingly. – Kelly Bundy Jan 19 '22 at 20:56