2

おそらく明らかですが、何らかの理由で、このコードは次のとおりです。

import random
import time
def tables():
    global tablesUsed
    tablesUsed = [int(x) for x in input("Please choose which multiplication tables you wish\nto practice, then type them like this: 2 5 10.\n").split()]
    return tablesUsed
def timer():
    timer  = input("Do you wish to play with the timer? (yes or no)\n")
    if timer == "yes":
        withTimer()
    else:
        withoutTimer()
def withTimer():
    playAgain = "yes"
    total = 0
    correct = 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)
        playAgain()
def withoutTimer():
    playAgain = "yes"
    total = 0
    correct = 0
    while playAgain == "yes":
        total = total + 1
        random1 = random.choice(tablesUsed)
        random2 = random.randint(1, 12)
        realAnswer = random1 * random2
        humanAnswer = int(input("What is the answer to this multiplication sum?\n" + str(random1) + " * " + str(random2) + "\n"))
        if realAnswer == humanAnswer:
            correct = correct + 1
            score = str(int(correct / total * 100)) + "%"
            print("Congratulations, you got it correct!\nScore: " + score)         
        else:
            score = str(int(correct / total * 100)) + "%"
            print("Unforunately, you got this one incorrect, the actual answer was " + str(realAnswer) + ".\nScore: " + score)
        playAgain()
def playAgain():
    playAgain = input("Do you wish to play again? (yes or no)\n")
    if playAgain == "yes":
        settings()
    else:
        print("Thank you for practising your multiplication tables with me. Your final score was " + score + " and your average time was " + averageTime)
def settings():
    settings = input("Do you wish to edit settings? (yes or no)\n")
    if settings == "yes":
        tables()
        timer()
tables()
timer()

次のようなエラーを返します。

TypeError: 'str' object is not callable, line 66, line 10, line 35

誰かが私が間違っていることを助けて教えてもらえますか?関数の定義が間違っていることが原因だと思いますが、問題を解決するものが見つかりません。

4

1 に答える 1

6

playAgain関数と関数のローカル変数の両方として定義しましたwithTimer

def withTimer():
    playAgain = "yes"

    # ...

    while playAgain == "yes":
        # ....

        playAgain()   # this is now a string, not the function

そうしないでください。関数名を隠さない意味のある名前を使用してください。

于 2013-02-23T15:02:34.117 に答える