Python 从另一个函数获取变量的值
我无法从问题函数中获取答案变量来检查答案是否正确 以下是我迄今为止所做的工作:Python 从另一个函数获取变量的值,python,function,variables,Python,Function,Variables,我无法从问题函数中获取答案变量来检查答案是否正确 以下是我迄今为止所做的工作: import random import operator a = 0 ops = { '+':operator.add, '-':operator.sub } def generateQuestion(): x = random.randint(1, 10) y = random.randint(1, 10) op = random.choice(list(ops.keys(
import random
import operator
a = 0
ops = {
'+':operator.add,
'-':operator.sub
}
def generateQuestion():
x = random.randint(1, 10)
y = random.randint(1, 10)
op = random.choice(list(ops.keys()))
a = ops.get(op)(x,y)
print("What is {} {} {}?\n".format(x, op, y))
return a
def askQuestion():
guess = input("")
if guess == a:
print("Correct")
else:
print("Wrong, the answer is ", a)
generateQuestion()
askQuestion()
我剪掉了中间的东西,一切都很好
我知道我在顶部也设置了a=0,否则我会得到一个未定义的错误。但是,我无法从generateQuestion()之外获得数学结果。您可以通过将答案值直接传递到
askQuestion
中来减轻对全局的需要
如果您正在运行Python3,请不要忘记将input
的返回值转换为int。如果使用python 2,则不需要int
转换
import random
import operator
ops = {
'+':operator.add,
'-':operator.sub
}
def generateQuestion():
x = random.randint(1, 10)
y = random.randint(1, 10)
op = random.choice(list(ops.keys()))
a = ops.get(op)(x,y)
print("What is {} {} {}?\n".format(x, op, y))
return a
def askQuestion(a):
guess = input("")
try:
if int(guess) == a:
print("Correct")
else:
print("Wrong, the answer is ", a)
except:
print('Did not input integer')
askQuestion(generateQuestion())
通过将答案值直接传递到
askQuestion
中,您可以减少对全局搜索的需要
如果您正在运行Python3,请不要忘记将input
的返回值转换为int。如果使用python 2,则不需要int
转换
import random
import operator
ops = {
'+':operator.add,
'-':operator.sub
}
def generateQuestion():
x = random.randint(1, 10)
y = random.randint(1, 10)
op = random.choice(list(ops.keys()))
a = ops.get(op)(x,y)
print("What is {} {} {}?\n".format(x, op, y))
return a
def askQuestion(a):
guess = input("")
try:
if int(guess) == a:
print("Correct")
else:
print("Wrong, the answer is ", a)
except:
print('Did not input integer')
askQuestion(generateQuestion())
你有一个范围问题。您的变量a
内部generateQuestion
与外部a
变量不同
为了解决这个问题,您需要在generateQuestion
内将a
声明为全局
import random
import operator
a = 0
ops = {
'+':operator.add,
'-':operator.sub
}
def generateQuestion():
x = random.randint(1, 10)
y = random.randint(1, 10)
op = random.choice(list(ops.keys()))
global a
a = ops.get(op)(x,y)
print("What is {} {} {}?\n".format(x, op, y))
return a
def askQuestion():
guess = input("")
if guess == a:
print("Correct")
else:
print("Wrong, the answer is ", a)
generateQuestion()
askQuestion()
看看这个:
您有一个范围界定问题。您的变量a
内部generateQuestion
与外部a
变量不同
为了解决这个问题,您需要在generateQuestion
内将a
声明为全局
import random
import operator
a = 0
ops = {
'+':operator.add,
'-':operator.sub
}
def generateQuestion():
x = random.randint(1, 10)
y = random.randint(1, 10)
op = random.choice(list(ops.keys()))
global a
a = ops.get(op)(x,y)
print("What is {} {} {}?\n".format(x, op, y))
return a
def askQuestion():
guess = input("")
if guess == a:
print("Correct")
else:
print("Wrong, the answer is ", a)
generateQuestion()
askQuestion()
看看这个:
解释一下为什么这样做很好。另外,不需要初始赋值
a=0
。谢谢,我仍然在使用python,现在a给出了正确的答案,但不管输入什么,它都给出了“错误答案”,解释了为什么这样做会很好。另外,a=0
的初始赋值是不需要的。谢谢,我还在使用python,现在a给出了正确的答案,但不管我输入什么,它都会在python 2中给出“错误答案”input
解析您输入的任何文本,因此如果您输入3
,您将得到一个int,无需强制转换。鉴于OP使用的是print
函数(并且没有从\uuuuuu future\uuuuuuuu
导入),我假设了Python 3。是的,Python 3.4,第一次使用Python,我通常使用unity脚本来完成我的项目,感谢您的回答,以及Python 2input
解析您输入的任何文本,因此,如果您输入3
,您将得到一个int,无需强制转换。鉴于OP使用的是print
函数(并且没有从\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu未来导入),我假设为Python 3。是的Python 3.4,第一次使用Python