0

Hi I'm a beginner programmer. I don't know how can I call a variable from function. I have two def calcular() and guardar(). I get some variables from calcular() that I will call later, but when I call variables in guardar(), it tells me that variable is not defined. I tried making global var, but it doesn't work. Hope you can help me

This is a little of my code...

def calcular():

    if nClient == "":
        texto = ("Inserta Numero de cliente")
        ventanaMensaje(texto)
    else:
        if cl1=="":
            texto = ("Inserta Clave")
            ventanaMensaje(texto)       
        else:
            if aB1 == "":
                texto = ("Inserta Cantidad")
                ventanaMensaje(texto)
            else:
                try:
                    clt = open("preciosEsp.txt","r+")
                    lClt = clt.readlines()
                    rClt = lClt[0]
                    sClt = rClt.split("'")
                    nRClt = sClt[0]

                    if nClient == nRClt:
                        cReg=sClt[1]

                        if cl1== cReg:
                                prc=sClt[2]
                         else:
                            k=1
                            while cl1 != cReg:
                                cReg=sClt[k]
                                k=k+2
                                if cl1== cReg:
                                    ñ=k-1
                                    prc=sClt[ñ]
                    else:
                       x = 0
                        while nClient != nRClt:
                            rClt = lClt[x]
                            sClt = rClt.split("'")
                            nRClt = sClt[0]
                            x=x+1
                            if nClient == nRClt:
                                cReg=sClt[1]
                                if cl1==cReg:
                                    prc=sClt[2]
                                else:
                                    k=1
                                    while cl1 != cReg:
                                        cReg=sClt[k]
                                        k=k+2
                                        if cl1== cReg:
                                            ñ=k-1
                                            prc=sClt[ñ]

                    indice=int(prc)+3

                    pdcts = open("productos.txt","r+")
                    lPdcts = pdcts.readlines()
                    rPdcts = lPdcts[0]
                    sPdcts= rPdcts.split("'")
                    nPdcts = sPdcts[0]
                    t = 0
                    if cl1 == nPdcts:
                        precio1=sPdcts[indice]
                        global txtD1################## MAKE A GLOBAL VAR 
                        txtD1=sPdcts[1]  #### THIS IS THE VARIABLE ########
def guardar():

    guardarDatos = (n+txtD1)  ################# I CALL HERE, BUT TELL ME THAT VARIABLE IS NOT DEFINED

2 Answers 2

3

If you really want a global variable, you'd define it outside of any function

txtD1 = None
def calcular():
    ...

it will then exist at module level. However, globals are rarely (read: never) the solution you should be using, instead you should be returning information from functions rather than modifying global state. You'd then pass that information into another function to use.

The global keyword in python says that you are referencing a global variable, not creating a new one. However, in your code no such name exists, so you're not actually referencing anything.

Sign up to request clarification or add additional context in comments.

Comments

1

first create your "database" somewhere global

clt = dict(map(lambda x:x.split(" ",1),open("preciosEsp.txt","r+"))

now you can acess it anywhere with

clt.get(nClient)

next calcular should return the values you want

def calcular():
    ...
    precio = clt.get(nClient)
    return [precio,nClient,...]

then you would store the returned values (or do something with them as soon as they are returned )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.