def main():
text = input("Please enter text")
translated = caesar(text)
print(text,"ciphered is", translated)
def caesar(text):
key = int(input("Please enter key shift"))
key = key % 26
translated = ""
for letter in text:
if letter.isalpha():
num = ord (letter)
num += key
if letter.isupper():
if num > ord ('Z'):
num -=26
elif num < ord ('A'):
num += 26
elif letter.islower():
if num > ord ('z'):
num -= 26
elif num < ord ('a'):
num += 26
translated += chr(num)
else:
translated += letter
return translated
main()
到目前为止,我有这个,但它实际上并没有改变这个词,例如,如果我将移位长度设为3,“Hello”转移到“o”,“a”不会转移在任何人都可以帮助我吗?
答案 0 :(得分:0)
参考:“Caesar Cipher with Python”http://inventwithpython.com/chapter14.html
def main():
text = input("Please enter text: ")
translated = caesar(text)
print(text,"ciphered is", translated)
def caesar(message):
key = int(input("Please enter key shift: "))
key = -key
translated = ''
for symbol in message:
if symbol.isalpha():
num = ord(symbol)
num += key
if symbol.isupper():
if num > ord('Z'):
num -= 26
elif num < ord('A'):
num += 26
elif symbol.islower():
if num > ord('z'):
num -= 26
elif num < ord('a'):
num += 26
translated += chr(num)
else:
translated += symbol
return translated
main()