我正在尝试让程序运行,直到用户不键入"No“。下面是我已经完成的代码:
print("What language would you prefer?\n e for English, s for spanish, f for french")
choice= input("Your choice\n")
english= "Hello iso-3166-2: en-us"
french= "Salut! iso-3166-2:fr"
spanish= "Ola iso-3166-2:es"
if choice == "e":
print (english)
elif choice == "f":
print (french)
elif choice == "s":
print (spanish)
while True:
res= input("Do you want to choose another language? Yes/No:")
if res == "No":
break发布于 2019-07-10 16:00:17
english= "Hello iso-3166-2: en-us"
french= "Salut! iso-3166-2:fr"
spanish= "Ola iso-3166-2:es"
print("What language would you prefer?\n e for English, s for spanish, f for french")
choice = input("Your choice\n")
while True:
if choice == "e":
print(english)
elif choice == "f":
print(french)
elif choice == "s":
print(spanish)
res = input("Do you want to choose another language? Yes/No:")
if res == 'Yes':
print("What language would you prefer?\n e for English, s for spanish, f for french")
choice = input("Your choice\n")
elif res == 'No':
print ("Goodbye")
break输出:
What language would you prefer?
e for English, s for spanish, f for french
Your choice
e
Hello iso-3166-2: en-us
Do you want to choose another language? Yes/No:Yes
What language would you prefer?
e for English, s for spanish, f for french
Your choice
f
Salut! iso-3166-2:fr
Do you want to choose another language? Yes/No:No
Goodbye发布于 2019-07-10 15:39:58
您应该首先导入sys库,然后将其余代码放入While True循环中。在代码的末尾编写:
Answer = input ("Do you want to continue? (y/n)")
If Answer == 'n' :
sys.exit请注意,上面的代码是while true循环中的最后一段代码。
https://stackoverflow.com/questions/56965404
复制相似问题