Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_List_Variables_Global Variables_Pygame - Fatal编程技术网

从Python中另一个函数的列表中提取值

从Python中另一个函数的列表中提取值,python,list,variables,global-variables,pygame,Python,List,Variables,Global Variables,Pygame,我正在为一个机器人编程,我想使用pygame的Xbox控制器。到目前为止,这是我得到的(Daniel J.Gonzalez的原始代码学分): 你看到叫“出去”的名单了吗?其中的每个元素都是Xbox控制器上的一个按钮。我想提取这些元素并将它们放在变量上,每个元素/按钮一个变量,这样我就可以控制我的机器人 我怎么做呢? 我曾尝试使用全局变量,但后来一切都变得一团糟。 请注意,我是Python的初学者。如果您希望在程序中有out,那么只需从函数get返回它即可: def get(): # rest

我正在为一个机器人编程,我想使用pygame的Xbox控制器。到目前为止,这是我得到的(Daniel J.Gonzalez的原始代码学分):

你看到叫“出去”的名单了吗?其中的每个元素都是Xbox控制器上的一个按钮。我想提取这些元素并将它们放在变量上,每个元素/按钮一个变量,这样我就可以控制我的机器人

我怎么做呢? 我曾尝试使用全局变量,但后来一切都变得一团糟。
请注意,我是Python的初学者。

如果您希望在程序中有
out
,那么只需从函数
get
返回它即可:

def get():
  # rest of the code ...
  return out
同时更改功能测试:

def test():
    while True:
        out = get()
        LThumbstickX = out[0]
        LThumbstickY = out[1]
        # and so on
然后像以前一样运行程序。功能
测试
所做的是不断地(
为True
)读取键盘。例如,您可以执行以下操作:

def test():
    while True:
        out = get()
        LThumbstickX = out[0]
        if LThumbstickX != 0:
            print 'Left button has been pressed'
            # and so on

您只需返回列表并使用python的解包功能:

def get():
    out = [1,2,3,4]
    return out

first, second, third, fourth = get()

我这样做了,但现在我得到了这个错误:回溯(最近一次调用):文件“C:\Users\viniciusmunich\Desktop\gamepad.py”,第46行,in first=out[1]name错误:名称“out”未定义该错误与从函数中获取
out
无关。您没有初始化所需的所有内容(例如,您似乎需要一个窗口)
def get():
    out = [1,2,3,4]
    return out

first, second, third, fourth = get()