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.")

TA贡献1886条经验 获得超2个赞
如果你想完全退出程序你需要调用exit方法。
import sys
...
if answer_1 != Q1_answer:
print("Im sorry, You have lost the game.")
sys.exit()
添加回答
举报