RetainedFlame7 RetainedFlame7 - 1 year ago 51
Python Question

How do I check a user input against a pre-defined value when it can be an integer or a word?

import random
lives = 3
while lives > 0:
print("If the number is divisible by 3, type FIZZ (all caps)")
print("If the number is divisible by 5, type BUZZ (all caps)")
print("If the number is divisible by 3 and 5, type FIZZ BUZZ (all caps)")
print("If the number is none of the above, type the number")
print("You have three lives and then the game is over")
print("Try and beat me!")
for number in range(1,101):
if number % 3 == 0 and number % 5 == 0:
correct_answer = "FIZZ BUZZ"
elif number % 3 == 0:
correct_answer = "FIZZ"
elif number % 5 == 0:
correct_answer = "BUZZ"
correct_answer = number
first_go = random.randint(0,1)
if first_go == 0:
computer_go = True
computer_go = False
if computer_go == True:
print("CPU:", correct_answer)
computer_go == False
elif computer_go == False:
answer = input("Your go:")
if answer == correct_answer:
computer_go = True
print("Wrong answer!")
lives - 1

I tried to use the if statement 'if answer == correct_answer' but it doesn't seem to be working for values that are neither FIZZ, BUZZ or FIZZ BUZZ. I'm also having trouble regarding the while loop; the message 'wrong answer' is printed, however the while loop does not end after three wrong attempts as it should with lives - 1 resulting in lives becoming 0 and ending the while loop.

Answer Source

Instead of

correct_answer = number


correct_answer = str(number)

That way you're comparing strings to strings.

You're not changing the value of lives. Do

lives = lives - 1
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download