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 でコメントを設定できます。

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