0

我正在尝试使用 python,并且我制作了这个小数学游戏。虽然我在评分系统方面遇到了一些问题。每次玩家得到正确答案时,我希望分数增加 1。我已经尝试过,但是每次玩家正确回答时我都没有让它增加。这是代码

import operator
import random

operations = {
    "addition": ("+", operator.add),
    "substraction": ("-", operator.sub),
    "multiplication": ("*", operator.mul),
    "division": ("/", operator.floordiv),
}

def ask_operation(difficulty, maxtries=3):
    maxvalue = 5 * difficulty
    x = random.randint(1, maxvalue)
    y = random.randint(1, maxvalue)
    op_name, (op_symbol, op_fun) = random.choice(list(operations.items()))
    result = op_fun(x, y)
    score = 0

    print("Difficulty level %d" % difficulty)
    print("Now lets do a %s calculation and see how clever you are." % op_name)
    print("So what is %d %s %d?" % (x, op_symbol, y))

    for ntry in range(1, 1+maxtries):
        answer = int(input(">"))
        if answer == result:
            print("Correct!") 
            score += 1
            print score
            return True

        elif ntry == maxtries:
            print("That's %s incorrect answers.  The end." % maxtries)
        else:
            print("That's not right.  Try again.")
    return False

def play(difficulty):
    while ask_operation(difficulty):
        difficulty += 1
    print("Difficulty level achieved: %d" % difficulty)

play(1)
4

1 回答 1

2

每次在 中,分数都会重置为 0 ask_operation。您应该play改为初始化它。

顺便说一句,//Increment score//不是有效的 Python。你可以像这样在 Python 中设置注释,即使在 Stack Overflow 中也是如此。

score += 1 # Increment score
于 2013-10-23T19:30:46.150 回答