UnboundLocalError: локальная переменная 'key1', на которую ссылаются до назначения - PullRequest
0 голосов
/ 29 февраля 2020

так что ... в моей функции я хочу сделать диктонару из нескольких струн, но это не работает. ошибка в строке 104. вот код (извините за спам elif, но я любитель на python):

def decrypt(text):
    count = int(0)
    text = str(text)
    text = text.replace('',' ')
    text = text.split()
    text = { i : text[i] for i in range(0, len(text) ) }
    while not count == 26:
        random_num3 = randint(1, 3)
        random_num4 = randint(1, 4)
        if text.get(count) == 2:
            text = " ".join(text)
            if random_num3 == 1:
                text = text.replace('2', 'a')
            elif random_num3 == 2:
                text = text.replace('2', 'b')
            elif random_num3 == 3:
                text = text.replace('2', 'c')
            text = text.split()
            key1 = text
            key1 = key1.replace('0',' ')
        elif text.get(count) == 3:
            text = " ".join(text)
            if random_num3 == 1:
                text = text.replace('3', 'd')
            elif random_num3 == 2:
                text = text.replace('3', 'e')
            elif random_num3 == 3:
                text = text.replace('3', 'f')
            text = text.split()
            key2 = text
            key2 = key2.replace('0',' ')
        elif text.get(count) == 4:
            text = " ".join(text)
            if random_num3 == 1:
                text = text.replace('4', 'g')
            elif random_num3 == 2:
                text = text.replace('4', 'h')
            elif random_num3 == 3:
                text = text.replace('4', 'i')
            text = text.split()
            key3 = text
            key3 = key3.replace('0',' ')
        elif text.get(count) == 5:
            text = " ".join(text)
            if random_num3 == 1:
                text = text.replace('5', 'j')
            elif random_num3 == 2:
                text = text.replace('5', 'k')
            elif random_num3 == 3:
                text = text.replace('5', 'l')
            text = text.split()
            key4 = text
            key4 = key4.replace('0',' ')
        elif text.get(count) == 6:
            text = " ".join(text)
            if random_num3 == 1:
                text = text.replace('6', 'm')
            elif random_num3 == 2:
                text = text.replace('6', 'n')
            elif random_num3 == 3:
                text = text.replace('6', 'o')
            text = text.split()
            key5 = text
            key5 = key5.replace('0',' ')
        elif text.get(count) == 7:
            text = " ".join(text)
            if random_num4 == 1:
                text = text.replace('7', 'p')
            elif random_num4 == 2:
                text = text.replace('7', 'q')
            elif random_num4 == 3:
                text = text.replace('7', 'r')
            elif random_num4 == 4:
                text = text.replace('7', 's')
            text = text.split()
            key6 = text
            key6 = key6.replace('0',' ')
        elif text.get(count) == 8:
            text = " ".join(text)
            if random_num3 == 1:
                text = text.replace('8', 't')
            elif random_num3 == 2:
                text = text.replace('8', 'u')
            elif random_num3 == 3:
                text = text.replace('8', 'v')
            text = text.split()
            key7 = text
            key7 = key7.replace('0',' ')
        elif text.get(count) == 9:
            text = " ".join(text)
            if random_num4 == 1:
                text = text.replace('9', 'w')
            elif random_num4 == 2:
                text = text.replace('9', 'x')
            elif random_num4 == 3:
                text = text.replace('9', 'y')
            elif random_num4 == 4:
                text = text.replace('9', 'z')
            text = text.split()
            key8 = text
            key8 = key8.replace('0',' ')
        count += 1
    results = dict()
    results['1'] = key1
    results['2'] = key2
    results['3'] = key3
    results['4'] = key4
    results['5'] = key5
    results['6'] = key6
    results['7'] = key7
    results['8'] = key8
    return results

кстати я использую редактор thonny, а функция используется в другом файле python и Я использую только случайный модуль. ИДК, что сказать больше, просто спросите в комментариях, я скажу, что

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...