Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从另一个函数获取变量的值_Python_Function_Variables - Fatal编程技术网

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 2
input
解析您输入的任何文本,因此,如果您输入
3
,您将得到一个int,无需强制转换。鉴于OP使用的是
print
函数(并且没有从
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu未来导入),我假设为Python 3。是的Python 3.4,第一次使用Python