Python数增量
我正在试验python,我制作了这个小数学游戏。虽然我对评分系统有一些问题。每当玩家得到正确答案时,我希望分数增加1。我已经试过了,但是每次球员做对了什么,我都不能增加。 这是密码Python数增量,python,Python,我正在试验python,我制作了这个小数学游戏。虽然我对评分系统有一些问题。每当玩家得到正确答案时,我希望分数增加1。我已经试过了,但是每次球员做对了什么,我都不能增加。 这是密码 import operator import random operations = { "addition": ("+", operator.add), "substraction": ("-", operator.sub), "multiplication": ("*", operato
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)
每次在
ask\u操作中,分数都会重置为0。您应该在play
中对其进行初始化
顺便说一下,//Increment score//
不是有效的Python。您可以像这样在Python中设置注释,即使在堆栈溢出中也是如此
score += 1 # Increment score
你在文章中的缩进正确吗?不正确。我从文本编辑中复制并粘贴了它,所以我丢失了格式。我会编辑的。是的,请修改缩进。目前,您似乎对每个新问题都将分数设置为0://code>//Increment score//
不是PythonAh中的注释是的,很抱歉,我有时会混淆语法。谢谢,我如何在播放功能中初始化它?抱歉,我对python非常陌生。与您在ask\u操作中所做的操作相同。确保在while循环之前保存它,以便它只运行一次。