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!")