【发布时间】:2017-03-09 07:17:43
【问题描述】:
我正在尝试创建一个循环以使用户回到程序的开头。我无法让它打印“欢迎使用比较功能”。程序将运行,要求用户输入 1 和 2,然后打印比较的答案,但我不知道如何让它重新开始。
def comparison():
loop = True
while (loop):
print(" Welcome to the comparison function")
a = int (input("Enter your first number for value a, then hit enter key:"))
b = int (input("Enter your second number for value b, then hit enter key:"))
def compare (a,b):
if a>b:
return 1
elif a==b:
return 0
else:
return -1
answer = compare(a,b)
print (answer)
while True:
response=input ("Would you like to perform another comparison? (yes/no)")
if response=="yes" or response =="YES" or response =="Yes":
print ("Thank you!")
break
elif response=="no" or response=="NO" or response =="No":
loop=False
print ("Thank you, have a great day!")
break
else:
continue
【问题讨论】:
-
显示的代码会出现缩进错误。
-
是的,请编辑您的帖子,使其具有正确的缩进,因为它显示在您的代码中。
-
此代码在正确缩进时工作正常......所以你的问题是缩进。
-
假设您的代码中的缩进很好(否则它根本不会为您运行),您使用的是什么版本的 Python?我可以让它在 Python 2 中正常运行,但它需要将
input更改为raw_input。 -
我现在看到的代码在 Python 3.5 中运行得很好
标签: python while-loop