Python全局列表 [英] Python global lists

查看:148
本文介绍了Python全局列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习python,并且在全局变量/列表方面遇到了问题.我正在写一个基本的河内手册塔程序,这是当前的程序:

I'm learning python, and am having a problem with global variables/lists. I'm writing a basic manual tower of hanoi program, here's the program currently:

pilar1 = [5,4,3,2,1,0]
pilar2 = [0,0,0,0,0,0]
pilar3 = [0,0,0,0,0,0]

def tower_of_hanoi():

    global pillar1
    global pillar2
    global pillar3

    print_info()

def print_info():

    global pillar1
    global pillar2
    global pillar3

    for i in range(4,-1,-1):
        print(pillar1[i], " ", pillar2[i], " ", pillar3[i])

我尝试了一些变体,但是每次遇到错误"NameError:未定义全局名称'pillar1'".

I've tried a few variations, but every time I got the error "NameError: global name 'pillar1' is not defined".

在此设置下处理全局列表的最佳方法是什么?如果可能的话,我宁愿只使用一个源文件.谢谢!

What would be the best way to handle a global list in this setting? I'd prefer to just use one source file, if possible. Thanks!

推荐答案

这是因为您已将其声明"为pilar1,而不是pillar1

It's because you've "declared" it as pilar1, not pillar1

这篇关于Python全局列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆