我正在尝试制作一个脚本,将消息的每个字母旋转13个字母,从而创建一个简单的密码。我的密码正确旋转字母,我遇到的问题是它只将其加密为大写字母。
例如,对于我输入"你好!世界",它应该返回"乌里伊布!Jbeyq",但它的回归"URYYB !JBEYQ">
uppercase = ['A','B','C','D', 'E', 'F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
lowercase = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
ciphered = ""
for i in message:
if i.isalpha():
if i in uppercase: # checks if letter (i) is an upper case and treats it as such
letter = uppercase.index(i)
rotated = uppercase[(letter + 13) % 26] # used to loop through alphabet
ciphered = ciphered + rotated
else:
letter = lowercase.index(i)
rotated = uppercase[(letter + 13) % 26] # used to loop through alphabet
ciphered = ciphered + rotated
else:
ciphered = ciphered + i
return ciphered
小错误,在 else 条件下,您使用的是大写列表。将其更改为小写:
uppercase = ['A','B','C','D', 'E', 'F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
lowercase = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
ciphered = ""
for i in message:
if i.isalpha():
if i in uppercase: # checks if letter (i) is an upper case and treats it as such
letter = uppercase.index(i)
rotated = uppercase[(letter + 13) % 26] # used to loop through alphabet
ciphered = ciphered + rotated
else:
letter = lowercase.index(i)
rotated = lowercase[(letter + 13) % 26] # used to loop through alphabet
ciphered = ciphered + rotated
else:
ciphered = ciphered + i
return ciphered