如何在不进入下一行代码的情况下结束python程序

时间:2019-05-02 11:04:05

标签: python python-3.x if-statement while-loop

我希望当玩家拒绝时说再见然后结束,而转至选择类别。有什么建议吗?

while True:
    choice = input ("Do you want to play?")
    if choice == "yes": 
        print("Great!")
        break # Go to the game
    elif choice == "no":
        print("Goodbye.")
        break # nothing
    else:
        print("Please answer yes or no.")


continent = input("Choose a category: \n"
              "Africa \n"
              "Asia \n"
              "Antarctica \n"
              "Australia \n"
              "Europe \n"
              "North America \n"
              "South America \n")

这个问题有所不同,因为它没有询问循环,而是询问如何正确结束程序。

1 个答案:

答案 0 :(得分:1)

while True:
    choice = input ("Do you want to play?")
    if choice == "yes" 
        print("Great!")
        break # Go to the game
    elif choice == "no" 
        print("Goodbye.")
        break # nothing
    else:
        print("Please answer yes or no.")