我有一个函数可以测量某人回答乘法和所需的时间,但我希望能够显示用户完成使用该函数后所花费的平均时间(它在一个 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)
我已经编写了 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
请帮助我尝试了解发生了什么,以及解决方案!
提前致谢。