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

Python-每个函数运行时使用不同的变量

Python-每个函数运行时使用不同的变量,python,function,variables,unique,Python,Function,Variables,Unique,我会尽力解释清楚的。我创建了一个函数,它使用Tkinter的tkFileDialog.askopenfilename()来选择照片。脚本返回时带有两个变量中的坐标: def getCoordinates(): # code to get coordinates here # variables for lat and long global latitude global longitude latitude longitude 现在,我创

我会尽力解释清楚的。我创建了一个函数,它使用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()
def TwoPic():
运行=0
运行<2时:
运行=运行+1
getCoordinates()
FirstRunLat=纬度
FirstRunLong=经度
打印FirstRunLat+“”+FirstRunLong
SecRunLat=纬度
SecRunLong=经度
打印SecRunLat+“”+SecRunLong
第三纬度=纬度
ThirdRunLong=经度
打印ThirdRunLat+“”+Thirdrunlot
root.quit()
基本上会发生的是,SecRunLat和SecRunLong以及ThirdRunLat和ThirdRunLong最终将与FirstRunLat和FirstRunLong相同

所以我要问的是,如果我再次运行这个函数,我如何运行这个函数来获得坐标,并给变量赋予不同的名称,这些名称保持唯一,并且不会重复

希望这是有意义的

事先非常感谢

基本上会发生的是,
SecRunLat
&
SecRunLong
ThirdRunLat
&
ThirdRunLong
最终将与
FirstRunLat
FirstRunLong
相同

当然-您不会在这两者之间更改值,因为您只调用了一次
getCoordinates()

最好返回您的结果:

def getCoordinates():

    # code to get coordinates here

    return latitude, longitude # as a tuple.

def TwoPic():
    run = 0
    results = []
    while run < 2:
        run = run + 1

        lat, lng = getCoordinates()
        print lat + " " + lng
        results.append((lat, lng))
    print results

    root.quit()
def getCoordinates():
#在这里获取坐标的代码
以元组形式返回纬度、经度。
def TwoPic():
运行=0
结果=[]
运行<2时:
运行=运行+1
纬度,lng=getCoordinates()
打印纬度+“”+lng
结果。追加((lat,lng))
打印结果
root.quit()
基本上会发生的是,
SecRunLat
&
SecRunLong
ThirdRunLat
&
ThirdRunLong
最终将与
FirstRunLat
FirstRunLong
相同

当然-您不会在这两者之间更改值,因为您只调用了一次
getCoordinates()

最好返回您的结果:

def getCoordinates():

    # code to get coordinates here

    return latitude, longitude # as a tuple.

def TwoPic():
    run = 0
    results = []
    while run < 2:
        run = run + 1

        lat, lng = getCoordinates()
        print lat + " " + lng
        results.append((lat, lng))
    print results

    root.quit()
def getCoordinates():
#在这里获取坐标的代码
以元组形式返回纬度、经度。
def TwoPic():
运行=0
结果=[]
运行<2时:
运行=运行+1
纬度,lng=getCoordinates()
打印纬度+“”+lng
结果。追加((lat,lng))
打印结果
root.quit()

好吧,如果你想继续使用globals,我只会使用列表:

def TwoPic():
    run = 0
    Lats = []
    Longs = []
    while run < 2:
        run = run + 1
        getCoordinates()

        Lats.append(latitude)
        Longs.append(longitude)

    root.quit()
def TwoPic():
运行=0
Lats=[]
Longs=[]
运行<2时:
运行=运行+1
getCoordinates()
纬度附加(纬度)
Longs.append(经度)
root.quit()

好吧,如果你想继续使用globals,我只会使用列表:

def TwoPic():
    run = 0
    Lats = []
    Longs = []
    while run < 2:
        run = run + 1
        getCoordinates()

        Lats.append(latitude)
        Longs.append(longitude)

    root.quit()
def TwoPic():
运行=0
Lats=[]
Longs=[]
运行<2时:
运行=运行+1
getCoordinates()
纬度附加(纬度)
Longs.append(经度)
root.quit()
从函数返回值,并将其作为元组分配:

def getCoordinates():
    # code to get coordinates here

    return latitude, longitude
在调用这些时:

def TwoPic():
    run = 0
    while run < 2:
        run = run + 1

        FirstRunLat, FirstRunLong = getCoordinates()

        print FirstRunLat + " " + FirstRunLong

        SecRunLat, SecRunLong = getCoordinates()
        print SecRunLat + " " + SecRunLong

        ThirdRunLat, ThirdRunLong = getCoordinates()
        print ThirdRunLat + " " + ThirdRunLong

    root.quit()
def TwoPic():
运行=0
运行<2时:
运行=运行+1
FirstRunLat,FirstRunLong=getCoordinates()
打印FirstRunLat+“”+FirstRunLong
SecRunLat,SecRunLong=getCoordinates()
打印SecRunLat+“”+SecRunLong
ThirdRunLat,ThirdRunLong=getCoordinates()
打印ThirdRunLat+“”+Thirdrunlot
root.quit()
不要使用全局函数传递函数结果;这就是
return
语句的作用。

从函数中返回值,并将其指定为元组:

def getCoordinates():
    # code to get coordinates here

    return latitude, longitude
在调用这些时:

def TwoPic():
    run = 0
    while run < 2:
        run = run + 1

        FirstRunLat, FirstRunLong = getCoordinates()

        print FirstRunLat + " " + FirstRunLong

        SecRunLat, SecRunLong = getCoordinates()
        print SecRunLat + " " + SecRunLong

        ThirdRunLat, ThirdRunLong = getCoordinates()
        print ThirdRunLat + " " + ThirdRunLong

    root.quit()
def TwoPic():
运行=0
运行<2时:
运行=运行+1
FirstRunLat,FirstRunLong=getCoordinates()
打印FirstRunLat+“”+FirstRunLong
SecRunLat,SecRunLong=getCoordinates()
打印SecRunLat+“”+SecRunLong
ThirdRunLat,ThirdRunLong=getCoordinates()
打印ThirdRunLat+“”+Thirdrunlot
root.quit()

不要使用全局函数传递函数结果;这就是
return
语句的目的。

这是一种非常糟糕的方法。要么使用列表或字典来存储
FirstRunLat
和其他内容,要么只进行函数调用而不进行循环。这是一种非常糟糕的方法。可以使用列表或字典来存储
FirstRunLat
和其他内容,也可以不使用循环来执行函数调用。