到达特定行后如何停止程序?

我正在尝试用 python 创建一个游戏,一个名为“谁想成为百万富翁?”的多项选择游戏。问题是一旦用户未能回答第一个问题,我希望程序停止执行第二个问题。


print("What is the tallest being in existence?")


input(("If you want to see the choices just press enter. " + name))


print("Is it A, Dinosaur?")

print("B, The one and only, Giraffe")

print("C,The soul of the ocean Whale")

print("or D, None")


print("So what do you believe is the answer ? " + name + " ")


answer_1 = input()


if answer_1 == Q1_answer:

    print("Correct! You Have Earned 100,000$ ")

    score = +100000

if answer_1 != Q1_answer:

    print("Im sorry, You have lost the game.")


print("Which famous inventor was born in 1856?")


print("A Einstein")

print("B, Tesla")

print("C, Napoleon")

print("D, Newton")


answer_2 = input()


if answer_2 == Q2_answer.lower():


    print("Correct! It is Tesla Indeed, Your reached  200,000$ ")

    score = +100000

else:

    print("Sorry, wrong answer. You have lost and earned 0$. ")


绝地无双
浏览 116回答 3
3回答

紫衣仙女

我认为你可以用更好的方式编写你的程序,目前你不能轻易地添加问题,因为你必须为每个新问题重复整个代码。您可以将所有问题存储在一个列表中,然后遍历它们。sys.exit()由于我组织代码的方式,我也不需要。这是代码:questions = [    {        "question": "What is the tallest being in existence?",        "options": [            "Is it A, Dinosaur?",            "B, The one and only, Giraffe",            "C,The soul of the ocean Whale",            "or D, None"        ],        "correctOption": "a",        "prize": 100_000    },    {        "question": "Which famous inventor was born in 1856?",        "options": [            "A Einstein",            "B, Tesla",            "C, Napoleon",            "D, Newton"        ],        "correctOption": "b",        "prize": 100_000    }]isGameWon = Truescore = 0for question in questions:    print(question['question'])    input("If you want to see the choices just press enter.")    for option in question['options']:        print(option)    print("So what do you believe is the answer?")    answer = input()    if answer.lower() == question['correctOption']:        score = score + question['prize']        print("Correct! You Have Earned $" + str(score) + ".")    else:        isGameWon = False        breakif (isGameWon):    print("Congrats! You have won the game. You earned $" + str(score) + ".")else:    print("Sorry, wrong answer. You have lost and earned $0.")

MM们

如果你想完全退出程序你需要调用exit方法。import sys...    if answer_1 != Q1_answer:    print("Im sorry, You have lost the game.")    sys.exit()

白板的微信

您可以使用 exit() 退出程序。我会打印一条消息,告诉用户程序将退出。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python