Python-每个函数运行时都有不同的变量 [英] Python - different variable with each function run

查看:58
本文介绍了Python-每个函数运行时都有不同的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会尽力解释这一点.我创建了一个使用Tkinter的tkFileDialog.askopenfilename()允许您选择照片的函数.脚本返回了两个变量的坐标:

I'll try explain this as well as I can. I've created a function that uses Tkinter's tkFileDialog.askopenfilename() to let you select a photograph. The script comes back with the coordinates in two variables:

def getCoordinates():

    # code to get coordinates here

    # variables for lat and long
    global latitude
    global longitude

    latitude
    longitude

现在,我创建了一个函数,该函数调用TWICE的第一个函数getCoordinates().因此,这要求选择两张照片.

Now, I've created a function that calls the first function getCoordinates() TWICE. So this asks for TWO photographs to be selected.

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相同.

What basically will happen, is that the SecRunLat & SecRunLong and the ThirdRunLat & ThirdRunLong will end up being the same as FirstRunLat and FirstRunLong.

所以我要问的是,如何运行该函数以获取坐标并为变量赋予不同的名称,使其保持唯一性,如果再次运行该函数,则不会重复?

So what I'm asking is, how can I run the function to get coordinates and give the variables different names that stay unique and don't get duplicated if I run the function again?

希望这是有道理的.

非常感谢您.

推荐答案

好吧,如果您想继续使用全局变量,我只会使用列表:

Well, if you want to keep on using globals, I would just use lists:

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

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

    root.quit()

这篇关于Python-每个函数运行时都有不同的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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