为什么我的 python 代码只能用大写字母加密一条消息?
Why is my python code only ciphering a message with capitals?
我正在尝试制作一个脚本,将消息的每个字母旋转 13 个字母,从而创建一个简单的密码。我的密码正确地旋转了字母,我遇到的问题是它只将它加密为大写字母。
例如,我输入 "Hello ! World",它应该 return "Uryyb ! Jbeyq" 但它 returning "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
我正在尝试制作一个脚本,将消息的每个字母旋转 13 个字母,从而创建一个简单的密码。我的密码正确地旋转了字母,我遇到的问题是它只将它加密为大写字母。
例如,我输入 "Hello ! World",它应该 return "Uryyb ! Jbeyq" 但它 returning "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