我試圖讓一個字符串不斷重複,如果答案是錯誤的。我會如何去做這件事?我的代碼是低於哪些作品,但不重複答案。如果輸入錯誤答案,如何再次詢問字符串?
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