平均在 While 循环中的时间

0 投票
1 回答
1297 浏览
提问于 2025-04-17 16:53

我有一个函数,用来测量一个人回答乘法题所花的时间,但我想在用户完成使用这个函数后,能够显示他们的平均时间(这个函数是在一个循环里运行的)。下面是这个函数:

def withTimer():
    playAgain = "yes"
    correct = 0
    total = 0
    while playAgain == "yes":
        total = total + 1
        random1 = random.choice(tablesUsed)
        random2 = random.randint(1, 12)
        realAnswer = random1 * random2
        start = time.time()
        humanAnswer = int(input("What is the answer to this multiplication sum?\n" + str(random1) + " * " + str(random2) + "\n"))
        if realAnswer == humanAnswer:
            elapsed = round((time.time() - start), 1)
            correct = correct + 1
            score = str(int(correct / total * 100)) + "%"
            if elapsed < 2:
                print("Congratulations, you got it correct in " + str(elapsed) + " seconds!\nThat is a very good time!\nScore: " + score)
            else:
                print("Congratulations, you got it correct in " + str(elapsed) + " seconds!\nNow work on your time.\nScore: " + score)            
        else:
            score = str(int(correct / total * 100)) + "%"
            print("Unforunately, you got this one incorrect, the actual answer was " + str(realAnswer) + ".\nScore: " + score)
        elapsed1 = 
        playAgain = input("Do you wish to play again? (yes or no)\n")
        if playAgain == "yes":
            settings()
        else:
            print("Thank you for practicing your multiplication tables\nwith me\nFinal Score: " + score + "\nAverage Time: " + averageTime)

我写了一个叫做 averageTime 的变量,想在这里显示平均时间。
我尝试过这样做:

if realAnswer == humanAnswer:
            elapsed = round((time.time() - start), 1)
            times = times.append(elapsed)

我在另一个函数里定义了一个变量 times = [],并且用了 global times,但是这样会出现错误:

'NoneType' object has no attribute 'append', line 26

请帮我理解一下发生了什么,以及怎么解决这个问题!
提前谢谢你。

1 个回答

0

你可以先列出所有经过的时间,然后把这些时间加起来,最后除以列表里的项目数量,这样就能算出平均时间。不过其实你不需要这么麻烦。只要你一直记录总的经过时间,回答的平均时间就可以用总的经过时间除以总的回答数量来算。

下面这个版本的程序同时用这两种方法来计算平均时间,并把结果都显示出来,以证明这两种方法得到的结果是一样的。

import time,random

def withTimer():
    playAgain = "yes"
    correct = 0
    total = 0
    totalTime = 0
    etime = []
    while playAgain[0] == "y":
        total = total + 1
        random1 = random.randint(1, 12)
        random2 = random.randint(1, 12)
        realAnswer = random1 * random2
        start = time.time()
        humanAnswer = int(input("What is the answer to this multiplication sum?\n" + str(random1) + " * " + str(random2) + "\n"))
        if realAnswer == humanAnswer:
            elapsed = round((time.time() - start), 1)
            etime.append(elapsed)
            correct = correct + 1
            score = str(int(correct / total * 100)) + "%"
            if elapsed < 2:
                print("Congratulations, you got it correct in " + str(elapsed) + " seconds!\nThat is a very good time!\nScore: " + score)
            else:
                print("Congratulations, you got it correct in " + str(elapsed) + " seconds!\nNow work on your time.\nScore: " + score)            
        else:
            score = str(int(correct / total * 100)) + "%"
            print("Unforunately, you got this one incorrect, the actual answer was " + str(realAnswer) + ".\nScore: " + score)
        totalTime += elapsed
        playAgain = input("Do you wish to play again? (yes or no)\n")
    averageTime = totalTime / total
    averageTime1 = sum(etime) / len(etime)
    if playAgain[0] != "y":
        print("Thank you for practicing your multiplication tables\nwith me\nFinal Score: " + score + "\nAverage Time: " + str(averageTime) + " AverageTime1 = "  + str(averageTime))


withTimer()

撰写回答