希望能对您有所帮助!

以下是相关代码:

此代码将键盘初始化为3d数组:

KeyboardRow1 = ["`", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "="]
KeyboardRow2 = ["", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "[", "]", ""]
KeyboardRow3 = ["", "a", "s", "d", "f", "g", "h", "j", "k", "l", ";", ""]
KeyboardRow4 = ["", "z", "x", "c", "v", "b", "n", "m", ",", ".", "/"]
KeyboardRow1S = ["~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+"]
KeyboardRow2S = ["", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "{", "}", "|"]
KeyboardRow3S = ["","A", "S", "D", "F", "G", "H", "J", "K", "L", ":", ""]
KeyboardRow4S = ["", "Z", "X", "C", "V", "B", "N", "M", "<", ">", "?", ""]
Array2R = [KeyboardRow1, KeyboardRow2, KeyboardRow3, KeyboardRow4]
Array2S = [KeyboardRow1S, KeyboardRow2S, KeyboardRow3S, KeyboardRow4S]
Array3 = [Array2R, Array2S]


此代码要求用户输入一个字符串:

Password = getpass.getpass("Enter a password: ")


此代码查找密码的每个字符在数组中的位置,并将其在数组中的坐标值附加到KeyboardPositions中

i = 0
j = 0
k = 0
z = 0
KeyboardPositions = []

for z in range(0,PasswordLength):

    for i in range(0,1):

        if Password[z] == str(Array3[i][j][k]):
            KeyboardPositions.append((i,j,k))

        for j in range(0,2):

            if Password[z] == str(Array3[i][j][k]):
                KeyboardPositions.append((i,j,k))

            for k in range (0,12):

                if Password[z] == str(Array3[i][j][k]):
                    KeyboardPositions.append((i,j,k))


当我运行代码时,它可以在键盘的前两行正常工作。如果密码为qwerty,则数组的内容(按预期)为[[0,1,1),(0,1,2),(0,1,3),(0,1,4),( 0,1,5),(0,1,6)]。三个坐标中的第一个是是否保持移位,第二个是行,第三个是列。

如果我输入asdfg,zxcvb或ASD之类的内容将无法正常工作。 KeyboardPositions将为空。第三行或第四行中的任何内容以及保持移位的内容均不起作用。

最佳答案

问题是您的range参数。其中有两个错误。请记住,range(0, n)0n-1,并且不包括n本身。因此,range(0,1)仅包含0。对于range(0,2),我假设您想从0遍历到3(包括这两个端点),所以您想要range(0,4),或者只是range(4)

关于python - 将键盘表示为3D阵列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34484719/

10-14 12:39