我试图让一个字符串不断重复,如果答案是错误的。我会如何去做这件事?我的代码是低于哪些作品,但不重复答案。如果输入错误答案,如何再次询问字符串?
print("Hello there, what is your name?")
MyName = input()
print ("Nice to meet you " + MyName)
print ("What is 2 + 2?")
answer = input()
if answer is '4':
print("Great job!")
elif answer != '4':
print ("Nope! please try again.")
while answer != '4':
print ("What is 2 + 2?")
break
@ MooingRawr问题清楚地显示了正在使用的while循环。只有一个小错误阻止程序成功运行。 – tcooc
作为一个方面说明,请不要使用'is'来比较字符串是否相等,因为它不这样做。改用==代替。查看https://stackoverflow.com/questions/1504717/why-does-comparing-strings-in-python-using-either-or-is-sometimes-produce为什么。 – tcooc