首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >选择yes后,如何让程序再次运行?

选择yes后,如何让程序再次运行?
EN

Stack Overflow用户
提问于 2019-07-10 15:21:37
回答 2查看 54关注 0票数 0

我正在尝试让程序运行,直到用户不键入"No“。下面是我已经完成的代码:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-07-10 16:00:17

代码语言:javascript
复制
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

输出:

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2019-07-10 15:39:58

您应该首先导入sys库,然后将其余代码放入While True循环中。在代码的末尾编写:

代码语言:javascript
复制
Answer = input ("Do you want to continue? (y/n)")
If Answer == 'n' :
    sys.exit

请注意,上面的代码是while true循环中的最后一段代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56965404

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档