代表键盘作为3D阵列

时间:2015-12-27 20:52:08

标签: python arrays data-structures keyboard

希望你能帮忙!

以下是相关代码:

此代码将键盘初始化为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)。三个坐标中的第一个是是否保持shift,第二个是行,第三个是列。

如果我输入类似asdfg,zxcvb或ASD的内容,它将无效。 KeyboardPositions将为空。第三排或第四排的任何东西以及任何保持换档的东西都不起作用。

1 个答案:

答案 0 :(得分:2)

问题是你的range参数。它们中有几个错误。请注意,range(0, n)0n-1运行,并排除n本身。因此range(0,1)仅包含0。对于range(0,2),我假设您希望从03进行迭代,包括range(0,4)range(4)