我试图这样做,以便当用户键入数字4时,它应该说是正确或错误,但它会自动弹出哈哈,即使没有用户输入任何内容。老实说,我不知道如何修复它。
print("Welcome to the maths skills testing machine!")
userinput = input("\nAre you ready to test your maths skills?")
if userinput=="yes":
print("\nGreat!")
else:
print("\nWell we're doing it anyway!")
print("\nLets start with something basic...")
print("\nWhat is 2+2?")
userinput
if userinput=="4":
print("\nAhh that was way too easy...")
else:
print("\nHaha you are pretty bad")
我希望它等待用户写一个答案,然后告诉他们是对还是错。我觉得我需要使用一个字符串,因为它是一个数字但是?非常感谢任何帮助。
答案 0 :(得分:2)
如评论中所述,您需要再次请求新输入,并将其值分配给userinput
变量,否则您使用的是已输入的数据,在本例中为“是”,因此答案永远不会是“4”。
您需要更改此行:
print("\nWhat is 2+2?")
为:
userinput = input("\nWhat is 2+2?")
完整代码:
print("Welcome to the maths skills testing machine!")
userinput = input("\nAre you ready to test your maths skills?")
if userinput=="yes":
print("\nGreat!")
else:
print("\nWell we're doing it anyway!")
print("\nLets start with something basic...")
userinput = input("\nWhat is 2+2?")
if userinput=="4":
print("\nAhh that was way too easy...")
else:
print("\nHaha you are pretty bad")
祝你好运!