Python While循环上的平均时间

Python While循环上的平均时间,python,time,python-3.x,Python,Time,Python 3.x,我有一个函数,它测量一个人回答乘法和所需的时间,但我希望能够显示用户使用完该函数后所花费的平均时间(在while循环中)。以下是函数: def withTimer(): playAgain = "yes" correct = 0 total = 0 while playAgain == "yes": total = total + 1 random1 = random.choice(tablesUsed) rando

我有一个函数,它测量一个人回答乘法和所需的时间,但我希望能够显示用户使用完该函数后所花费的平均时间(在while循环中)。以下是函数:

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)
其中,我在另一个函数中定义了变量
times=[]
,以及
global times
,但这会产生错误:

'NoneType' object has no attribute 'append', line 26
请帮助我尝试并了解发生了什么,以及解决方案

提前感谢。

您可以创建一个已用时间列表,然后将所有已用时间相加并除以列表中的项目数来计算平均值,但不需要这样做。只要您保持运行的总时间,平均回答时间将是总时间除以总回答数

以下版本的程序将以这两种方式计算平均值,并显示这两种方式,以显示两种方式的结果相同

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()
导入时间,随机
def with timer():
再次播放=“是”
正确=0
总数=0
总时间=0
时间=[]
再次播放时[0]=“y”:
总计=总计+1
random1=random.randint(1,12)
random2=random.randint(1,12)
realAnswer=random1*random2
开始=时间。时间()
humanAnswer=int(输入(“这个乘法和的答案是什么?\n”+str(random1)+“*”+str(random2)+“\n”))
如果realAnswer==humanAnswer:
已用时间=轮((time.time()-start),1)
附加时间(已用)
正确=正确+1
分数=str(整数(正确/总数*100))+“%”
如果时间小于2:
打印(“祝贺您,您在“+str(已用)+”秒内就搞定了!\n这是一个非常好的时机!\n存储:“+score”)
其他:
打印(“祝贺您,您在“+str(已用)+”秒内答对了!\n现在开始计时。\n存储:“+score”)
其他:
分数=str(整数(正确/总数*100))+“%”
print(“不幸的是,您的答案不正确,实际答案是“+str(realswer)+”\n核心:“+score”)
总时间+=已用时间
playreach=输入(“您希望再次播放吗?(是或否)\n”)
平均时间=总时间/总时间
平均时间1=总和(时间)/长度(时间)
如果再次播放[0]!=“y”:
打印(“感谢您与我一起练习乘法表\n最终分数:“+Score+”\n平均时间:”+str(averageTime)+“AverageTime1=“+str(averageTime))
withTimer()

我想我可能需要创建一个在“总计”处插入“已用”的列表。我说的对吗?您导入了时间,然后有一个名为time的变量??你不认为可怜的ickle python可能会有点困惑吗?哦,对不起@vorspring,这个变量实际上被称为
times
,但这可能仍然是个坏名字
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()