Python Forum

Full Version: Problem with my code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello Guys,
i am working on a school project and could need some help.

command = [getuser(),getpw(),getnewuser(),getnewpw(),getreppw()]
variable = [user,pw,newU,newpw,reppw]
surce = [e1,e2,f1,f2,f3]


for i in range(5) :
    def command[i]:
        global variable[i]
        variable[i] = surce[i].get()
        return variable[i]
As you see i have to create lots of defs and like to shorten this,
because i have to add even more variables and it would be way more efficent if i
could just add it to the list, instead of creating a new def.

def getuser():
    global user
    user = e1.get()
    return user

def getpw():
    global pw
    pw = e2.get()
    return pw

def getnewuser():
    global newU
    newU = f1.get()
    return newU

def getnewpw():
    global newpw
    newpw = f2.get()
    return newpw

def getreppw():
    global reppw
    reppw = f3.get()
    return reppw


Thanks for your help.

Greedings
Godx
If I write your code in my python interpreter, it won't run. Can you post a whole working script, because I find it difficult to understand what you're trying to do.
Thats my problem, the upper code dosent run. It should basicly just shorten the lowere code.
(Aug-21-2018, 05:00 PM)Godx Wrote: [ -> ]
for i in range(5) :
    def command[i]:
        global variable[i]
        variable[i] = surce[i].get()
        return variable[i]

that makes no sense to me.
I'd like to create 5 defs with this.
In the first round it should get the first value of every string.

command = [getuser(),getpw(),getnewuser(),getnewpw(),getreppw()]
variable = [user,pw,newU,newpw,reppw]
surce = [e1,e2,f1,f2,f3]

so if 1 = 1:

def getuser():
    global user
    user = e1.get()
    return user
is there a file from which you read the data?
If I understand your intentions you want call function from string. This is how it's should be done: How do I use strings to call functions/methods?.

Defining function is not the same as calling function.