python保持得分在Definded功能中并返回



因此,我正在尝试进行一种与您可以选择的不同类别的测验。它可以正常工作,但我无法使得分数函数正常工作。如果我在三个正确的答案后打印(分数)(得分应为3),则仍然说0。如果我在得分 = 1之后放入回报分数,它将在一个正确的答案后自动停止,但我希望能够给出3个正确的答案。

下面是代码的一部分

   def quizquestion(score,question_category):
       for question_number,question in enumerate(question_category): 
           print ("Question",question_number+1)
           time.sleep(1)
           slow_type (question)
           for options in question_category[question][:-1]: 
               slow_type (options)
           user_choice = input("make your choice: ")
           if user_choice == question_category[question][-1]: 
               replay = good[random.randint(0,7)]
               slow_type (replay)
               score += 1
           else: 
               replay = wrong[random.randint(0,7)]
               slow_type (replay)
               slow_type ("The correct answer should have been")
               print(question_category[question][-1])
               time.sleep(1)
       slow_type("okay you finished this category, lets see what your score is in this category")
       slow_type("You have"), print(score), slow_type("correct answer(s)")
       return score

类别之一:

 questions_random = {
     "How tall is the Eiffel Tower?":['a. 350m', 'b. 342m', 'c. 324m', 'd. 1000ft','a'],
     "How loud is a sonic boom?":['a. 160dB', 'b. 175dB', 'c. 157dB', 'd. 213dB', 'd'],
     "What is the highest mountain in the world?":['a. Mont Blanc', 'b. K2', 'c. Mount Everest', 'd. Mount Kilomonjaro', 'c']
 } 

如果您需要更多代码来帮助我,请让我知道

分数可以是关键字参数。这样,它具有默认的初始值。这是一个从您的代码中得出的工作示例。

注意:为了进行测试,我用printgood[random.randint(0,7)]"ncorrect"替换slow_type,用"nwrongn"替换wrong[random.randint(0,7)]。这有效,返回正确的分数。

您可以从上一轮的问题中传递得分:

quizquestion(question_category, score=<current_score>)

您可以在此处了解有关关键字参数的更多信息。

def quizquestion(question_category, score=0):
    boundary = "*************************************************"
    for question_number,question in enumerate(question_category): 
        print ("nQuestion",question_number+1)
        time.sleep(1)
        print(question)
        for options in question_category[question][:-1]: 
            print(options)
    user_choice = input("make your choice: ")
    if user_choice == question_category[question][-1]: 
        replay = "ncorrect!"
        print(replay)
        score += 1
        print(boundary)
    else: 
        replay = "nwrongn"
        print(replay)
        print("The correct answer should have been")
        print(question_category[question][-1])
        print(boundary)
        time.sleep(1)
    print("nokay you finished this category, lets see what your score is in this category")
    print("You have {} correct answers!".format(score))
return score

最新更新