【发布时间】:2014-01-28 11:36:31
【问题描述】:
继续我之前的问题:Python - different variable with each function run
我现在可以多次运行该函数并附加到一个很棒的列表中。现在的问题是访问函数之外的列表。
def TwoPic():
run = 0
Lats = []
Longs = []
while run < 2:
run = run + 1
print ("Start func TwoPic")
oneMorePic()
Lats.append(finalLatSecondPhoto)
Longs.append(finalLongSecondPhoto)
print '\n\nYour coordinates: ' + rippedLatitudeTwo + ',-' + rippedLongitudeTwo
print "Below are the 2 new lats and longs in a list"
print Lats
print Longs
root.quit()
return Lats, Longs
root = Tk()
label = Label(root, text="Choose Iterations", font=('Calibri', 12))
button1 = Button(root, text = "0 Pics", command = Quit)
button2 = Button(root, text = "1 Pic", command = OnePic)
button3 = Button(root, text = "2 Pics", command = TwoPic)
label.pack()
button1.pack()
button2.pack()
button3.pack()
root.mainloop()
print "List: "
print Lats, Longs # Should print my list....I'm now outside of the function
错误:
Your coordinates: 43 48.54,-1 24.58
Below are the 2 new lats and longs in a list
[43.809, 43.809]
[1.409, 1.409]
List:
Traceback (most recent call last):
File "simonsScript.py", line 447, in <module>
print Lats, Longs
NameError: name 'Lats' is not defined
为什么不让我在函数之外打印列表?
【问题讨论】:
-
Lats和Longs在TwoPic()范围内定义它们不是全局的,您正在尝试访问未定义的变量。 -
在
print之前你需要打电话给Lats, Longs = TwoPic() -
"I can now run the function multiple times and append to a list which is great"请记住,在不同的函数调用中,您正在重新定义Lats、Longs列出内部函数。
标签: python list function tuples