Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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_Variables - Fatal编程技术网

Python 定义变量和执行变量

Python 定义变量和执行变量,python,variables,Python,Variables,此代码有一些问题。当我输入2尝试执行whaleq()时,它反而执行mathschallenge(),我只希望在用户输入1时执行 有人能帮我看看吗?下面的代码与python idle中的代码完全相同 print('The first thing you should think about doing is asking the locals if they have seen anything!') locallist = ['Ann-Marie, the butcher (1)',

此代码有一些问题。当我输入2尝试执行
whaleq()
时,它反而执行
mathschallenge()
,我只希望在用户输入1时执行

有人能帮我看看吗?下面的代码与python idle中的代码完全相同

print('The first thing you should think about doing is asking the locals if they have seen     anything!')

locallist = ['Ann-Marie, the butcher (1)',
             'Bella, the flourist (2)',
             'Caitlyn, the painter (3)',
             'Daniel, the scientist (4)',
             'Lauren, the Zookeeper (5)']
print(locallist)

localpick = int(input('Type the number of the local you want to talk to '))

def localx():
    if localpick == 1:
        mathschallenge()
    if localpick == 2:
        whaleq()

def mathschallenge():
    print('maths challenge yolo')
    quest = input('What is 10+10?')
    if quest == '20':
        print('correct')
    else:
        print('incorrect')

def whaleq():
    mammal = input('What is the largest mammal?')
    if mammal == 'whale':
        print('correct')
    else:
        print('incorrect')

input('Press the enter key to exit')

代码对我来说很好,除了重新组织一点之外,我感动了:

print('The first thing you should think about doing is asking the locals if they have seen     anything!')

locallist = ['Ann-Marie, the butcher (1)', 'Bella, the flourist (2)', 'Caitlyn, the 
painter         (3)', 'Daniel, the scientist (4)', 'Lauren, the Zookeeper (5)']
print(locallist)

localpick = int(input('Type the number of the local you want to talk to '))
从代码的顶部到底部,并在上面添加
localx()

input('Press the enter key to exit')
整个代码如下所示:

def localx():
    if localpick == 1:
        mathschallenge()
    if localpick == 2:
        whaleq()

def mathschallenge():
    print('maths challenge yolo')
    quest = input('What is 10+10?')
    if quest == '20':
        print('correct')
    else:
        print('incorrect')

def whaleq():
    mammal = input('What is the largest mammal?')
    if mammal == 'whale':
        print('correct')
    else:
        print('incorrect')


print('The first thing you should think about doing is asking the locals if they have seen     anything!')

locallist = ['Ann-Marie, the butcher (1)', 'Bella, the flourist (2)', 'Caitlyn, the painter     (3)', 'Daniel, the scientist (4)', 'Lauren, the Zookeeper (5)']
print(locallist)

localpick = int(input('Type the number of the local you want to talk to '))
localx()
input('Press the enter key to exit')
我希望这有帮助, ~z~博比


注意:这是在python 3.4.2中测试的,在从用户获取'localpick'值之后,您是否正在调用'localx'函数。我希望这不是您的确切代码。它没有正确缩进。将localx更改为localpick,并且没有做任何不同的操作。@RvdK我必须粘贴代码,然后在这里用4个空格缩进。因此,localpick可能是保存用户输入的变量。localx是一个函数,一旦你得到了你的用户选项,它就需要被调用,而这个选项是缺失的。所以,在从用户处获取输入后,您需要调用函数localx。感谢@bobbeh和alfasin的帮助