亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

絕地無雙 2023-06-20 10:38:25
我正在嘗試用 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 = +100000if 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 = +100000else:    print("Sorry, wrong answer. You have lost and earned 0$. ")
查看完整描述

3 回答

?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

我認為你可以用更好的方式編寫你的程序,目前你不能輕易地添加問題,因為你必須為每個新問題重復整個代碼。您可以將所有問題存儲在一個列表中,然后遍歷它們。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 = True

score = 0


for 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

        break


if (isGameWon):

    print("Congrats! You have won the game. You earned $" + str(score) + ".")

else:

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


查看完整回答
反對 回復 2023-06-20
?
MM們

TA貢獻1886條經驗 獲得超2個贊

如果你想完全退出程序你需要調用exit方法。


import sys

...    

if answer_1 != Q1_answer:

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

    sys.exit()


查看完整回答
反對 回復 2023-06-20
?
白板的微信

TA貢獻1883條經驗 獲得超3個贊

您可以使用 exit() 退出程序。我會打印一條消息,告訴用戶程序將退出。



查看完整回答
反對 回復 2023-06-20
  • 3 回答
  • 0 關注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號