我正在尝试使用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)
答案 0 :(得分:2)
每次ask_operation
时,分数都会重置为0。您应该在play
中初始化它。
顺便说一句,//Increment score//
不是有效的Python。您可以像这样在Python中设置注释,即使在Stack Overflow中也是如此。
score += 1 # Increment score