如何阻止用户输入无效的文字并引发错误?

时间:2019-04-28 02:35:28

标签: python

我正在制作一个Fizzbuzz游戏[请参见下面的代码]我想在if语句中添加一个条件,如果用户输入数字以外的任何内容,它将使用户返回到函数的开头,但它不能正常工作无论如何还是给我一个错误,有没有办法我可以做到这一点?

def fizzbuzz():
   print(f"\nEnter a number {name}.\n")

   number = int(input('> '))

   if number % 3 == 0:
      print("We got a FIZZ! And I ain't talking about the kid from B2k! You a bad MF!")
   elif number % 5 == 0:
      print("Buzz lightyear to star command. We are in the mist of a BAMF!")
   elif number % 3 == 0 and number % 5 == 0:
      print("FIZZBUZZ!" * 10, " FIZ [*head explodes*] [kid picks up the mic] How's that for Television")
   elif number / number != 1 or number == 0:
      print("Someones a little slow. Try again sweetheart.")
    fizzbuzz()
   else:
      print("Do you kiss your mother with that none Fizzed or buzzed mouth? Shame! Sercurity!")

print("Join us next time for the show where you better bring your fizzbuzz buzz fizz!......Or else.\n\n")

1 个答案:

答案 0 :(得分:0)

尝试使用try and except处理错误。