Python Forum
Problem with my code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Problem with my code (/thread-12361.html)



Problem with my code - Godx - Aug-21-2018

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


RE: Problem with my code - Gribouillis - Aug-21-2018

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.


RE: Problem with my code - Godx - Aug-21-2018

Thats my problem, the upper code dosent run. It should basicly just shorten the lowere code.


RE: Problem with my code - Axel_Erfurt - Aug-21-2018

(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.


RE: Problem with my code - Godx - Aug-21-2018

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



RE: Problem with my code - Axel_Erfurt - Aug-21-2018

is there a file from which you read the data?


RE: Problem with my code - perfringo - Aug-22-2018

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.