我搜索了网页和这个网站,整天都在闲逛,尝试了100种方法来让这个简单的小程序正常工作。我正在练习无休止的While循环和字符串用户输入。有人能解释我做错了什么吗?非常感谢。
while True:
print("This is the start.")
answer = input("Would you like to continue? (Y/N) ")
answer = answer.islower()
if answer == "n":
print("Ok thank you and goodbye.")
break
elif answer == "y":
print("Ok, let's start again.")
else:
print("You need to input a 'y' or an 'n'.")
您的代码有一个错误answer.islower()
将返回布尔值True或False,但您希望将其转换为较低的值,因此正确的方法将是answer.lower()
while True:
print("This is the start.")
answer = input("Would you like to continue? (Y/N) ")
answer = answer.lower() # change from islower() to lower()
if answer == "n":
print("Ok thank you and goodbye.")
break
elif answer == "y":
print("Ok, let's start again.")
else:
print("You need to input a 'y' or an 'n'.")
您只需要对此行进行一次修改:
代替
answer=answer.slow((
更改为
answer=answer.lower((