我试图创建一个循环,以便让用户回到程序的开头。我无法让它打印“欢迎使用比较功能”。程序将运行,要求用户输入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 3中函数的一个替换项:
如果您使用的是python 2.7:
^{pr2}$希望这有帮助。谢谢
相关问题 更多 >
编程相关推荐