当用户输入可以是整数或单词时,如何根据预定义值检查用户输入?

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"
      else:
          correct_answer = number
      first_go = random.randint(0,1)
      if first_go == 0:
          computer_go = True
      else:
          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
          else:
              print("Wrong answer!")
              lives - 1

我尝试使用 if 语句 'if answer == correct_answer' 但它似乎不适用于既非 FIZZ、BUZZ 或 FIZZ BUZZ 的值。我在 while 循环方面也遇到了麻烦;消息 'wrong answer' 被打印出来,但是 while 循环在三次错误尝试后并没有结束,因为 lives - 1 导致 lives 变为 0 并结束 while 循环。

而不是

correct_answer = number

correct_answer = str(number)

这样你就可以将字符串与字符串进行比较。

您没有更改 lives 的值。做

lives = lives - 1