为什么python不会在此函数中添加

时间:2019-06-11 00:50:52

标签: python python-3.x

我正在尝试制造一种初学者级的谜题机器,但转子却出现问题,在第一次遇到字母后转子没有增加。下面是出现上述问题的函数

def rotating(rotate_a, rotate_b, rotate_c, add_amount):
    rotate_a += 1
    print("")
    if rotate_a == 27:
        rotate_a = 0
        rotate_b += 1
        add_amount += 1
        rotate_a_b_c(b1, add_amount)
    else:
        rotate_a_b_c(a1, add_amount)
        add_amount += 1
        rotate_a += 1
    if rotate_b == 27:
        rotate_b = 0
        rotate_c += 1
        add_amount += 1
        rotate_a_b_c(c1, add_amount)
    else:
        add_amount += 1
    if rotate_c == 27:
        rotate_c = 0
        add_amount += 1
    else:
        add_amount += 1
    print(rotate_a, rotate_b, rotate_c)

我希望此方法的工作方式是还有另一个函数加密,一旦它加密了一个字母,它将去移动转子(在原始代码的底部)(add_ammount是一个不重要的小功能),您可以看到什么打印出所有的1和0

C:\Users\jgola\AppData\Local\Programs\Python\Python37-32\python.exe "C:/Users/jgola/PycharmProjects/Game/enigma matchine.py"
word to be coded(NO CAPS): abcd efgh ijkl mnop qrst uvwx yz ab cder ghij klmn opqr stuv wxwz

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0

1 0 0
bxtu disw amlj gvqy rcze okfh npbx tudc swam ljgv qyrc zeok fhfp 

Process finished with exit code 0

这是谜机器的输出(我们将重点放在1和0上),每次打印一组新的数字时,它的数量应该增加,但不是。为什么要这么做?我该怎么做才能解决这个问题?

def coding(a, b, c, d):
    add_amount = 0
    roa = 0
    rob = 0
    roc = 0
    final = str()
    var = input("word to be coded(NO CAPS): ")
    var = word_preparing(var)
    var_len = int(len(var) / 4)
    for z in range(0, var_len):
        x = z * 4
        for l in range(0, 4):
            start = ord(var[l+x])-97
            for i in range(0, 26):
                if a[start][1] == b[i][0]:
                    start = i
                    break
            for i in range(0, 26):
                if b[start][1] == c[i][0]:
                    start = i
                    break
            for i in range(0, 26):
                if c[start][1] == d[i][0]:
                    start = i
                    break
            for i in range(0, 26):
                if d[start][1] == c[i][0]:
                    start = i
                    break
            for i in range(0, 26):
                if c[start][1] == b[i][0]:
                    start = i
                    break
            for i in range(0, 26):
                if b[start][1] == a[i][0]:
                    start = i
                    end = chr(a[start][1]+96)
                    final += end
                    rotating(roa, rob, roc, add_amount)
                    a = [(a1[0], 22), (a1[1], 24), (a1[2], 15), (a1[3], 8), (a1[4], 10), (a1[5], 9), (a1[6], 26),
                         (a1[7], 16), (a1[8], 2), (a1[9], 21), (a1[10], 20),
                         (a1[11], 1), (a1[12], 6), (a1[13], 18), (a1[14], 25), (a1[15], 19), (a1[16], 5), (a1[17], 14),
                         (a1[18], 17), (a1[19], 3), (a1[20], 23),
                         (a1[21], 7), (a1[22], 13), (a1[23], 4), (a1[24], 12), (a1[25], 11)]
                    b = [(b1[0], 15), (b1[1], 5), (b1[2], 24), (b1[3], 16), (b1[4], 13), (b1[5], 4), (b1[6], 3),
                         (b1[7], 20), (b1[8], 25), (b1[9], 6), (b1[10], 23),
                         (b1[11], 21), (b1[12], 9), (b1[13], 14), (b1[14], 26), (b1[15], 19), (b1[16], 17), (b1[17], 1),
                         (b1[18], 11), (b1[19], 8), (b1[20], 12),
                         (b1[21], 7), (b1[22], 22), (b1[23], 2), (b1[24], 18), (b1[25], 10)]
                    c = [(c1[0], 9), (c1[1], 11), (c1[2], 6), (c1[3], 14), (c1[4], 13), (c1[5], 8), (c1[6], 22),
                         (c1[7], 26), (c1[8], 16), (c1[9], 2), (c1[10], 5),
                         (c1[11], 25), (c1[12], 23), (c1[13], 21), (c1[14], 17), (c1[15], 24), (c1[16], 20), # the reason this is all derpy is because I copied this from mobile to comuter
                         (c1[17], 18), (c1[18], 3), (c1[19], 19), (c1[20], 15),
                         (c1[21], 1), (c1[22], 10), (c1[23], 12), (c1[24], 4), (c1[25], 7)]
                    break
        final += " "
    print(final)

1 个答案:

答案 0 :(得分:0)

您正在将变量的值传递给函数,而不是变量本身。这些副本仅在函数运行时存在,并且更改它们不会影响原始变量的值,即使它们具有相同的名称。

尝试添加

key

位于函数顶部,紧接def语句之后。

还要从def语句和对该函数的所有调用中删除变量。

global rotate_a, rotate_b, rotate_c