python - 我们可以只使用函数在 python 中创建一个测验并跟踪分数吗
问题描述
我想创建一个函数来跟踪每个问题的分数,然后在另一个函数中使用它,该函数将最终结果返回给用户。我不知道该怎么做。
# Function for question 1
def ask_question_one(difficulty):
score = 0
print("What is the capital city of Canada?")
answer = input()
if difficulty <= 3:
if answer == "Ottawa" or answer == "ottawa":
score = score + 10
print("Your score is:", score)
else:
print(score)
elif difficulty > 3 and difficulty <= 7:
if answer == "Ottawa" or answer == "ottawa":
score = score + 30
print ("your score is:", score)
else:
print("You have given an incorrect answer. You lose 5 points.")
score = score - 5
print("Your score is:", score)
else:
count = 1
while count < 3:
answer = input()
if answer != "Ottawa" or answer != "ottawa":
score = score - 5
print("Your score is:", score)
score = score + 50
print("Your score after two tries is:", score)
def display_result(difficulty, score):
if difficulty <= 3 and score > 30:
print("Your difficulty level is", str(difficulty) + " and your score is", str(score))
# Top-level code.
set_difficulty = int(input("Please enter a difficuty level between 1 and 10: "))
if set_difficulty == "":
print("You did not enter a difficulty level. Try again! Exiting quiz.")
exit()
elif set_difficulty == 0:
print("0(zero) is not a valid difficulty. Exiting quiz.")
exit()
elif set_difficulty > 10:
print("Select a difficulty level less than 10.")
exit()
else:
print("....Let's begin the quiz....")
ask_question_one(set_difficulty)
这是我到目前为止所做的
解决方案
请正确解释评论。然后也许我可以回答(我把这个放在这里是因为我没有足够的声誉来发表评论:'()。
推荐阅读
- angular - Capacitor 3 并且没有导出成员 PushNotification
- php - Symofny:多次出现输入
- karma-jasmine - Angular Firestore jasmine 测试条件在 colleciton 中
- django - 是否可以在制作 DjangoModelFactory 时获取工厂男孩 Faker 对象的值以用于另一个字段
- css - CSS - 使用 max 为特定长度的 minmax 时,网格自动调整不起作用
- html - Flexbox 在增加项目宽度的同时保持包裹点
- typescript - 如何从 TypeScript 中的字符串值数组推断类型?
- python - Pydantic:导出具有不同编码的相同类型的字段
- php - 使用 Jenkins Pipeline 将 PHP 部署到 Azure Web App 时出错
- macos - 找到了 Libfreetype.6.dylib,但架构错误