![](/img/trans.png)
[英]Trying to loop back to the start of the program when the user answers “Yes”
[英]while loop back to start of program
我正在嘗試創建一個循環,以使用戶回到程序的開頭。 我無法打印“歡迎使用比較功能”。 該程序將運行,要求用戶輸入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中的函數:
def comparison():
while True:
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:"))
# Recommended replacement for cmp(a, b) in Python 3
# https://docs.python.org/3.0/whatsnew/3.0.html#ordering-comparisons
answer = (a > b) - (a < b)
print(answer)
response = input("Would you like to perform another comparison? (yes/no)")
while response.lower() not in ['yes', 'no']:
response = input("please enter proper response: ")
if response.lower() == "yes":
print("Thank you!")
else:
print("Thank you, have a great day!")
break
comparison()
def comparision():
loop = True
while loop:
print ("welcome to the comparision function: ")
a = int(input(("Enter your number for a value a , then hit enter key:")))
b = int (input("Enter your second number for value b, then hit enter key:"))
answer = ''
if a > b:
answer = 1
elif a == b:
answer = 0
else:
answer = -1
print (answer)
response = str(input("Would you like to perform another comparison? (yes/no) :"))
while response.lower() not in ['yes', 'no']:
response = str(input("please enter proper response: "))
if response.lower() == 'yes'
continue
elif response.lower() == 'no'
print ("Thank you, have a great day!")
break
if __name__ == '__main__':
comparision()
如果您使用的是python 2.7:
response = str(raw_input("Would you like to perform another comparison? (yes/no) :"))
希望這可以幫助。 謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.