誰かが掛け算の合計に答えるのにかかる時間を測定する関数がありますが、ユーザーが関数の使用を終了してからかかる平均時間を表示できるようにしたいと考えています (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
何が起こっているのかを理解し、解決策を教えてください!
前もって感謝します。