首页 > 解决方案 > 我们可以只使用函数在 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)

这是我到目前为止所做的

标签: python

解决方案


请正确解释评论。然后也许我可以回答(我把这个放在这里是因为我没有足够的声誉来发表评论:'()。


推荐阅读