0

誰かが掛け算の合計に答えるのにかかる時間を測定する関数がありますが、ユーザーが関数の使用を終了してからかかる平均時間を表示できるようにしたいと考えています (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

何が起こっているのかを理解し、解決策を教えてください!
前もって感謝します。

4

1 に答える 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()
于 2013-02-24T20:20:57.843 に答える