Home > other >  Python says "UnboundLocalError: local variable 'key' referenced before assignment&quo
Python says "UnboundLocalError: local variable 'key' referenced before assignment&quo

Time:07-05

i understand that this error happens when a variable gets mentioned before its defined but "key" is assigned to its value. I started learning python a week ago so i am sorry if my question has a really simple answer.

the code:

from stat import SF_APPEND
import time
import random
keyType = 0
key = 0
deCypherKey = 0
operationType = 0
needToLoop = True
alphabet = "abcdefghijklmnopqrstuvwxyz "
print("ogulSifreleyici v1.0")
time.sleep(0.3)
print("Ipucu: Hem Sifrelemek Hem de Desifrelemek Icin Programi 2 Kere Calistirabilirsiniz")




def cypher():
    message = input("Mesajini Gir:\n")
    message = message.lower()
    unCypheredMessageLength = len(message)
    letterOfMessageInQueue = 0
    keyStr = str(key)
    keyStrInt = 0
    whichOrderOfAlphabet = 0
    whichLetterOfAlphabet = " "
    whichDigitOfKey = 0
    orderOfCypheredLetter = 0
    cypheredLetter = "a"
    cypheredMessageList = []
    cypheredStr = ""
    while unCypheredMessageLength > 0:
        whichLetterOfAlphabet = alphabet[whichOrderOfAlphabet]
        if message[letterOfMessageInQueue] == whichLetterOfAlphabet:
            print("match")
            print(whichOrderOfAlphabet, message[letterOfMessageInQueue], whichLetterOfAlphabet)
            keyStrInt = int(keyStr[whichDigitOfKey])
            orderOfCypheredLetter = whichOrderOfAlphabet   keyStrInt
            if orderOfCypheredLetter > 26:
                orderOfCypheredLetter = orderOfCypheredLetter - 26
            cypheredLetter = alphabet[orderOfCypheredLetter]
            cypheredMessageList.append(cypheredLetter)

            unCypheredMessageLength = unCypheredMessageLength - 1
            letterOfMessageInQueue = letterOfMessageInQueue   1
            whichOrderOfAlphabet = 0
            whichDigitOfKey = whichDigitOfKey   1
            if whichDigitOfKey > 4:
                whichDigitOfKey = whichDigitOfKey - 5
            if len(cypheredMessageList) == len(message):
                    cypheredStr = "".join(cypheredMessageList)
                    print("Sifrelenmis Mesajiniz:\n"   cypheredStr)
                    time.sleep(1)
                    lastUserAnswer = input("1-Sifrele(Ayni Anahtar)  2-Sifrele(Farkli Anahtar)\n")
                    if lastUserAnswer == "1":
                        cypher()
                    if lastUserAnswer == "2":
                        key = input("Anahtar Giriniz(5 Haneli Sayi):\n") 
                        while len(str(key)) != 5:
                            key = input("Lutfen Bes Haneli Bir Sayi Giriniz\n")
                            if len(str(key)) == 5:
                                cypher()
                        cypher()  




        else:
            whichOrderOfAlphabet = whichOrderOfAlphabet   1 
            



def deCypher():
    deCypherMessage = input("Sifreli Mesajinizi Giriniz:\n")        








operationType = input("1-Sifrele  2-DeSifrele\n")
while needToLoop == True:
    if operationType == "1":
        keyType = input("1-Anahtar gir(5 haneli sayi)  2-Rastgele Anahtar\n")
        if keyType == "1":
            key = input("Anahtar Giriniz:\n")
            while len(str(key)) != 5:
                key = input("Lutfen Bes Haneli Bir Sayi Giriniz\n")
                if len(str(key)) == 5:
                    needToLoop = False
                    cypher()
        needToLoop = False        
        cypher()        
        if keyType == "2":
            key = int(random.uniform(10000, 100000))
            print("Anahtariniz: "   str(key))
            cypher()
            needToLoop = False

        else:
            print("Lutfen Seceneklerden Birini Seciniz")
            needToLoop = True 
    if operationType == "2":
        deCypherKey = input("Anahtarinizi Giriniz:\n")        
        while len(str(deCypherKey)) != 5:
                key = input("Lutfen Bes Haneli Bir Sayi Giriniz\n")
                if len(str(key)) == 5:
                    needToLoop = False
                    deCypher()
        needToLoop = False        
        deCypher()
    else:
        print("Lutfen Seceneklerden Birini Seciniz")
        needToLoop = True

CodePudding user response:

this is not exactly the reason you wrote, the fact is that the function cannot see variables that are outside the function. In order for the function to see them, you need to pass the variable as an argument to the function. That is, you need to change line 86 like this: cypher(key). But as you can see, this will give an error, because your function initially does not accept any arguments, in order to fix this we need to add the key argument in line 16 like this: def cypher(key):. There is the site where you can read more about it https://levelup.gitconnected.com/5-types-of-arguments-in-python-function-definition-e0e2a2cafd29

  • Related