我正试图在家里复制那些警戒密码 当这个运行时,结果关键字比它应该移动少一个地方?任何帮助??
虽然是真的: Alphabet =' ABCDEFGHIJKLMNOPQRSTUVWXYZ' #these是可以输入的字母
def main():
Mode = input("Press 1 for encryption or 2 for decyrption:")#this will allow the user to select whether to encode or decode
if Mode == '1':#this will allow user to encrypt when 1 is pressed
myMessage = input ("Enter a message to encrypt:")#this allows the user to enter the message they would like to be encrypted
myKey = input("Enter a keyword to use:")#this allows the user to enter what keyword they would like to encypt by
x = encryptM(myKey, myMessage)
elif Mode == '2':#this will allow user to decrypt when 2 is pressed
myMessage = input ("Enter a message to decrypt:")#this allows the user to enter the message they would like to be encrypted
myKey = input("Enter a keyword to use:")#this allows the user to enter what keyword they would like to encypt by
x = decryptM(myKey, myMessage)
print("Your encrypted message is: " +x ) #this will print the encrypted or decrypted message to the user
def encryptM(key, message):
return translateM(key, message, 'encrypt')
def decryptM(key, message):
return translateM(key, message, 'decrypt')
def translateM(key, message, mode):
x = []
keyIndex = 0
key = key.upper()
for symbol in message:
num = Alphabet.find(symbol.upper())
if num != -1:
if mode == 'encrypt':
num += Alphabet.find(key[keyIndex])#this will add the number from the keyword with the encryption number
elif mode == 'decrypt':
num -= Alphabet.find(key[keyIndex])#this will take away the number from the keyword from the decryption number
num %= len(Alphabet)#this converts the alphabet string into numbers
#this function will add the alphabet string to the end of the key so they can be added
if symbol.isupper():
x.append(Alphabet[num])
elif symbol.islower():
x.append(Alphabet[num].lower())
keyIndex += 1
if keyIndex == len(key):
keyIndex = 0
return "".join(x)
if __name__ == '__main__':
main( )
print (' ')#this adds a line between the loops
答案 0 :(得分:0)
问题是Alphabet.find
返回数组中的索引(即你的Alphabet
字符串),并且Python中字符的索引是从零开始的。
繁殖:
symbol = 'a'
Alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
num = Alphabet.find(symbol.upper())
print num
答案 1 :(得分:0)
我建议您使用字典、If 循环和列表来包含信息。我会告诉你如何制作这个代码。
首先,我们必须创建一个移位字典函数,稍后你会发现原因:
def shift_dict(Caesar, Shift):
dic_len = len(Caesar)
Shift = Shift % dic_len
list_dic = [(k,v) for k, v in iter(Caesar.items())]
Shifted = {
list_dic[x][0]: list_dic[(x - Shift) % dic_len][1]
for x in range(dic_len)
}
return Shifted
好的,现在我们将创建字典,“Shift-ee's”,这些列表将有助于代码左右: 将不断变化的转变:
Viginere = {
"A":0,
"B":1,
"C":2,
"D":3,
"E":4,
"F":5,
"G":6,
"H":7,
"I":8,
"J":9,
"K":10,
"L":11,
"M":12,
"N":13,
"O":14,
"P":15,
"Q":16,
"R":17,
"S":18,
"T":19,
"U":20,
"V":21,
"W":22,
"X":23,
"Y":24,
"Z":25
}
这是班次,班次的代码。 这是 VFU,即 Viginere - For - Upper,它将返回大写的大写加密文本:
VFU = {
0:"A",
1:"B",
2:"C",
3:"D",
4:"E",
5:"F",
6:"G",
7:"H",
8:"I",
9:"J",
10:"K",
11:"L",
12:"M",
13:"N",
14:"O",
15:"P",
16:"Q",
17:"R",
18:"S",
19:"T",
20:"U",
21:"V",
22:"W",
23:"X",
24:"Y",
25:"Z"
}
接下来,VFL,Viginere - For - Lower:
VFL = {
0:"a",
1:"b",
2:"c",
3:"d",
4:"e",
5:"f",
6:"g",
7:"h",
8:"i",
9:"j",
10:"k",
11:"l",
12:"m",
13:"n",
14:"o",
15:"p",
16:"q",
17:"r",
18:"s",
19:"t",
20:"u",
21:"v",
22:"w",
23:"x",
24:"y",
25:"z"
}
现在,我们将创建主体: 变量:
Asker = int(input("Do you want to... 1. Encode, or 2. Decode? "))
X = 0
Lister = []
Text = list(str(input("")))
Key = list(str(input("")).upper().replace(" ", "")
Z = 0
编码:
if Asker == 1:
for i in range(len(Text)):
Shift = Viginere[Key[(Z % len(Key))]]
if Text[X].isalpha():
LetterNum = Viginere[Text[X].upper()]
Helper = (LetterNum + Shift) % 26
if Text[X].isupper():
Lister.append(VFU[Helper])
else:
Lister.append(VFL[Helper])
else:
Lister.append(Text[X])
Z -= 1
X += 1
Z += 1
print(*Lister, sep = "")
现在让我们分解这段代码,看看它是如何工作的: for 循环运行整个文本长度:
Shift = Viginere[Key[(Z % len(Key))]]
这提供了转变,您可以轻松地分解它。 现在我们对字典进行移位,然后将其恢复为正常,如下代码所示:
if Text[X].isalpha():
LetterNum = Viginere[Text[X].upper()]
Helper = (LetterNum + Shift) % 26
if Text[X].isupper():
Lister.append(VFU[Helper])
else:
Lister.append(VFL[Helper])
else:
Lister.append(Text[X])
Z -= 1
X += 1
Z += 1
print(*Lister, sep = "")
这是解码的代码:
elif Asker == 2:
for i in range(len(Text)):
Shift = Viginere[Key[(Z % len(Key))]]
if Text[X].isalpha():
LetterNum = Viginere[Text[X].upper()]
Helper = (LetterNum - Shift) % 26
if Text[X].isupper():
Lister.append(VFU[Helper])
else:
Lister.append(VFL[Helper])
else:
Lister.append(Text[X])
Z -= 1
X += 1
Z += 1
print(*Lister, sep = "")
现在,当我们运行组合代码时:
def shift_dict(Caesar, Shift):
dic_len = len(Caesar)
Shift = Shift % dic_len
list_dic = [(k,v) for k, v in iter(Caesar.items())]
Shifted = {
list_dic[x][0]: list_dic[(x - Shift) % dic_len][1]
for x in range(dic_len)
}
return Shifted
Viginere = {
"A":0,
"B":1,
"C":2,
"D":3,
"E":4,
"F":5,
"G":6,
"H":7,
"I":8,
"J":9,
"K":10,
"L":11,
"M":12,
"N":13,
"O":14,
"P":15,
"Q":16,
"R":17,
"S":18,
"T":19,
"U":20,
"V":21,
"W":22,
"X":23,
"Y":24,
"Z":25
}
VFU = {
0:"A",
1:"B",
2:"C",
3:"D",
4:"E",
5:"F",
6:"G",
7:"H",
8:"I",
9:"J",
10:"K",
11:"L",
12:"M",
13:"N",
14:"O",
15:"P",
16:"Q",
17:"R",
18:"S",
19:"T",
20:"U",
21:"V",
22:"W",
23:"X",
24:"Y",
25:"Z"
}
VFL = {
0:"a",
1:"b",
2:"c",
3:"d",
4:"e",
5:"f",
6:"g",
7:"h",
8:"i",
9:"j",
10:"k",
11:"l",
12:"m",
13:"n",
14:"o",
15:"p",
16:"q",
17:"r",
18:"s",
19:"t",
20:"u",
21:"v",
22:"w",
23:"x",
24:"y",
25:"z"
}
Asker = int(input("Do you want to... 1. Encode, or 2. Decode? "))
X = 0
Lister = []
Text = list(str(input("")))
Key = list(str(input("")).upper().replace(" ", "")
Z = 0
if Asker == 1:
for i in range(len(Text)):
Shift = Viginere[Key[(Z % len(Key))]]
if Text[X].isalpha():
LetterNum = Viginere[Text[X].upper()]
Helper = (LetterNum + Shift) % 26
if Text[X].isupper():
Lister.append(VFU[Helper])
else:
Lister.append(VFL[Helper])
else:
Lister.append(Text[X])
Z -= 1
X += 1
Z += 1
print(*Lister, sep = "")
elif Asker == 2:
for i in range(len(Text)):
Shift = Viginere[Key[(Z % len(Key))]]
if Text[X].isalpha():
LetterNum = Viginere[Text[X].upper()]
Helper = (LetterNum - Shift) % 26
if Text[X].isupper():
Lister.append(VFU[Helper])
else:
Lister.append(VFL[Helper])
else:
Lister.append(Text[X])
Z -= 1
X += 1
Z += 1
print(*Lister, sep = "")
这变成了:
Do you want to... 1. Encode, or 2. Decode? 1
Please encode, as you always have worked!
Python is Awesome
Ejxhgr mfckhw, oe cds tskngk hwzw kavzcw!
还有:
Do you want to... 1. Encode, or 2. Decode? 2
Ejxhgr mfckhw, oe cds tskngk hwzw kavzcw!
Python is awesome
Please encode, as you always have worked!
感谢这个非常好的问题。我意识到这是在创建它的 5 年后,但我刚刚加入 StackOverflow,我在中学,我刚刚找到并解决了这个问题。
请告诉这是否有帮助,感谢您提出这个惊人的问题!