Search code examples
pythonglobal-variables

I am having this referenced before assignment error in my code and I can't get it to work even after implementing the "global"


[RESOLVED] I can't seem to get it to work as I've done as they've said in [this post][1], it was previously working until I added the "BfToAA" section, which is a modified copy of the "BfToARN", I don't think I got any error as this global fix was enough for before, I leave all my code next for you to see and maybe give advice on making it work.

#This is my "main"
def ElegirCamino():
    
    #here you can input your brainF*ck code that is going to be translated
    print("Inserte usted su código en notación especificada en el  README.txt")
    códigoBf = input()
    
    #here the code is sent to be translated to ARN by the convention that I've made.
    i=0
    print("El anterior código traducido a ARN según los datos datos en el README.txt es:")
    while i<len(códigoBf):
        BfToARN(códigoBf[i], i)
        i += 1
    códigoARN = códigoARN + "UAA"
    print(códigoARN)


    #here the code is sent to be translated to nucleic acids by the convention that I've made.
    i=0
    print("Lo que en ácidos nucléicos es:")
    while i<len(códigoBf):
        BfToAA(códigoBf[i], i)
        i += 1
    códigoAA = códigoAA + "STOP"
    print(códigoAA)

    

def BfToARN(Bf, i):
    if i==0:
        global códigoARN 
        códigoARN = "AUG"

    if(Bf == '+'):
        códigoARN = códigoARN + "UUU"
    elif(Bf == '-'):
        códigoARN = códigoARN + "UUA"
    elif(Bf == '.'):
        códigoARN = códigoARN + "UCU"
    elif(Bf == ','):
        códigoARN = códigoARN + "UAU"
    elif(Bf == '['):
        códigoARN = códigoARN + "UGU"
    elif(Bf == ']'):
        códigoARN = códigoARN + "UGG"
    elif(Bf == '<'):
        códigoARN = códigoARN + "CCU"
    elif(Bf == '>'):
        códigoARN = códigoARN + "CGU"
    else:
        print("    ERROR. Se esperaba uno de los caracteres del lenguaje Bf pero se ha encontrado un: \""+ Bf + "\"")

def BfToAA(Bf, i):
    if i==0:
        global códigoAA 
        códigoAA = "Met "

    if(Bf == '+'):
        códigoAA = códigoAA + "Phe "
    elif(Bf == '-'):
        códigoAA = códigoAA + "Leu "
    elif(Bf == '.'):
        códigoAA = códigoAA + "Ser "
    elif(Bf == ','):
        códigoAA = códigoAA + "Tyr "
    elif(Bf == '['):
        códigoAA = códigoAA + "Cys "
    elif(Bf == ']'):
        códigoAA = códigoAA + "Trp "
    elif(Bf == '<'):
        códigoAA = códigoAA + "Pro "
    elif(Bf == '>'):
        códigoAA = códigoAA + "Arg "
    else:
        print("    ERROR. Se esperaba uno de los caracteres del lenguaje Bf pero se ha encontrado un: \""+ Bf + "\"")

ElegirCamino()

Thanks to everybody that anwsered and helped me understeand my mistakes here. [1]: https://es.stackoverflow.com/questions/171863/local-variable-referenced-before-assignment


Solution

  • Your main mistake is assigning globals in the condition statement and not including the global keywords in your "main" function. I modified your code so that it runs. Take notice of where the global keyword is in both of the functions.

    
    
    #This is my "main"
    def ElegirCamino():
        global códigoARN
        global códigoAA
        #here you can input your brainF*ck code that is going to be translated
        print("Inserte usted su código en notación especificada en el  README.txt")
        códigoBf = input()
    
        #here the code is sent to be translated to ARN by the convention that I've made.
        i=0
        print("El anterior código traducido a ARN según los datos datos en el README.txt es:")
        while i<len(códigoBf):
            BfToARN(códigoBf[i], i)
            i += 1
        códigoARN = códigoARN + "UAA"
        print(códigoARN)
    
    
        #here the code is sent to be translated to nucleic acids by the convention that I've made.
        i=0
        print("Lo que en ácidos nucléicos es:")
        while i<len(códigoBf):
            BfToAA(códigoBf[i], i)
            i += 1
        códigoAA = códigoAA + "STOP"
        print(códigoAA)
    
        
    
    def BfToARN(Bf, i):
        global códigoARN 
    
        if i==0:
            códigoARN = "AUG"
        if(Bf == '+'):
            códigoARN = códigoARN + "UUU"
        elif(Bf == '-'):
            códigoARN = códigoARN + "UUA"
        elif(Bf == '.'):
            códigoARN = códigoARN + "UCU"
        elif(Bf == ','):
            códigoARN = códigoARN + "UAU"
        elif(Bf == '['):
            códigoARN = códigoARN + "UGU"
        elif(Bf == ']'):
            códigoARN = códigoARN + "UGG"
        elif(Bf == '<'):
            códigoARN = códigoARN + "CCU"
        elif(Bf == '>'):
            códigoARN = códigoARN + "CGU"
        else:
            print("    ERROR. Se esperaba uno de los caracteres del lenguaje Bf pero se ha encontrado un: \""+ Bf + "\"")
    
    def BfToAA(Bf, i):
        global códigoAA 
        if i==0:
            códigoAA = "Met "
        if(Bf == '+'):
            códigoAA = códigoAA + "Phe "
        elif(Bf == '-'):
            códigoAA = códigoAA + "Leu "
        elif(Bf == '.'):
            códigoAA = códigoAA + "Ser "
        elif(Bf == ','):
            códigoAA = códigoAA + "Tyr "
        elif(Bf == '['):
            códigoAA = códigoAA + "Cys "
        elif(Bf == ']'):
            códigoAA = códigoAA + "Trp "
        elif(Bf == '<'):
            códigoAA = códigoAA + "Pro "
        elif(Bf == '>'):
            códigoAA = códigoAA + "Arg "
        else:
            print("    ERROR. Se esperaba uno de los caracteres del lenguaje Bf pero se ha encontrado un: \""+ Bf + "\"")
    
    ElegirCamino()