【问题标题】:python keeping score in definded function and return itpython在定义函数中记分并返回
【发布时间】:2018-05-07 01:39:09
【问题描述】:

因此,我正在尝试进行一种测验,您可以从中选择不同的类别。它工作得很好,但我不能让分数功能正常工作。如果我在三个正确答案后打印(分数)(分数应该是 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']
 } 

如果您需要更多代码来帮助我,请告诉我

【问题讨论】:

  • 请发布一个 mcve 删除所有不相关的代码并包括函数 id 的调用方式。
  • 我无法重现该错误。用print 替换slow_type 并删除replay 的东西后,代码对我来说可以正常工作。最好发布一个完整的例子。另外,score 函数是什么意思?没有使用此名称的函数或函数调用。

标签: python python-3.x function spyder enumerate


【解决方案1】:

分数可以是关键字参数。这样它就有一个默认的初始值。这是一个从您的代码派生的工作示例。

注意:出于测试目的,我将slow_type 替换为print,将good[random.randint(0,7)] 替换为"\ncorrect",并将wrong[random.randint(0,7)] 替换为"\nwrong\n"。 这行得通,返回正确的分数。

您可以将上一轮问题的分数传递如下:

quizquestion(question_category, score=<current_score>).

您可以了解更多关于关键字参数here

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 = "\nwrong\n"
        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

【讨论】:

  • 非常感谢您的反馈,并对代码中的许多废话表示抱歉。下次我会,正如 bruno desthuilliers 所描述的,发布更多的 MCVE 代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-03
  • 1970-01-01
相关资源
最近更新 更多