【发布时间】:2026-01-12 19:45:02
【问题描述】:
我会尽力解释这一点。我创建了一个函数,它使用 Tkinter 的 tkFileDialog.askopenfilename() 让您选择一张照片。脚本返回两个变量中的坐标:
def getCoordinates():
# code to get coordinates here
# variables for lat and long
global latitude
global longitude
latitude
longitude
现在,我创建了一个函数,它两次调用第一个函数 getCoordinates()。所以这要求选择两张照片。
def TwoPic():
run = 0
while run < 2:
run = run + 1
getCoordinates()
FirstRunLat = latitude
FirstRunLong = longitude
print FirstRunLat + " " + FirstRunLong
SecRunLat = latitude
SecRunLong = longitude
print SecRunLat + " " + SecRunLong
ThirdRunLat = latitude
ThirdRunLong = longitude
print ThirdRunLat + " " + ThirdRunLong
root.quit()
基本上会发生什么,SecRunLat & SecRunLong 和 ThirdRunLat & ThirdRunLong 最终将与 FirstRunLat 和 FirstRunLong 相同。
所以我要问的是,我如何运行该函数来获取坐标并为变量赋予不同的名称,这些名称保持唯一并且在我再次运行该函数时不会重复?
希望这是有道理的。
非常感谢您。
【问题讨论】:
-
这是一个非常糟糕的方法。要么使用列表或字典来存储
FirstRunLat和其余部分,要么只在没有循环的情况下进行函数调用。
标签: python function variables unique