凯撒密码翻译错误
Caesar Cipher Translating Incorrectly
凯撒密码偏移量为 21,字母表为“abcdefghijklmnopqrstuvwxyz0123456789”。
例如“Xvzcvb”应该等于“Caesar”,但输出却是“CaeNaM”
ALPHABET = "abcdefghijklmnopqrstuvwxyz0123456789."
def decrpytstr(message):
#declare variables
newString = str()
ALPHABET = int()
oneLetter = str()
newAlphabetValue = int()
#loop to assign new alphabet value
for index in range(0, len(message)):
oneLetter = message[index]
ALPHABET = ord(oneLetter)
#assinging new alphabet value
newAlphabetValue = ALPHABET - 21
#creating the new string
newString = newString + chr(newAlphabetValue)
return newString
def main():
message = input("Enter text: ")
decrpytstr(message)
newString = decrpytstr(message)
print(newString)
input("Press ENTER to exit")
main()
https://www.asciitable.com/
正如我看到的 ascii table,b 98
的值将是 M 77
递减 21。我猜你必须分别处理大写和小写。
[已编辑]我不太明白你想达到什么目的,但是这个怎么样?
ALPHABET = "abcdefghijklmnopqrstuvwxyz"
def decrpytstr(message):
new_string = ""
for i in range(len(message)):
character = message[i]
new_character = ALPHABET[ALPHABET.find(character) - 21 % 26]
new_string = new_string + new_character
return new_string
def main():
message = input("Enter text: ")
decrpytstr(message)
newString = decrpytstr(message)
print(newString)
input("Press ENTER to exit")
main()
Enter text: xvznvm
caesar
Press ENTER to exit
有几个问题
- 您实际上并没有使用原来的字母表值
- 您没有考虑大写字符
- 我们需要环绕,这样当我们减去神奇的 21 步时,我们总能找到字母表中的东西
更正这些给我们这样的东西...
ALPHABET = "abcdefghijklmnopqrstuvwxyz0123456789."
def decrpytstr(message):
newString = ''
#loop to assign new alphabet value
for index in range(0, len(message)):
oneLetter = message[index]
#assinging new alphabet value
newAlphabetValue = (ALPHABET.find(oneLetter.lower()) - 21) % len(ALPHABET) + (ord('A') if oneLetter.isupper() else ord('a'))
#creating the new string
newString = newString + chr(newAlphabetValue)
return newString
def main():
message = input("Enter text: ")
decrpytstr(message)
newString = decrpytstr(message)
print(newString)
input("Press ENTER to exit")
main()
凯撒密码偏移量为 21,字母表为“abcdefghijklmnopqrstuvwxyz0123456789”。
例如“Xvzcvb”应该等于“Caesar”,但输出却是“CaeNaM”
ALPHABET = "abcdefghijklmnopqrstuvwxyz0123456789."
def decrpytstr(message):
#declare variables
newString = str()
ALPHABET = int()
oneLetter = str()
newAlphabetValue = int()
#loop to assign new alphabet value
for index in range(0, len(message)):
oneLetter = message[index]
ALPHABET = ord(oneLetter)
#assinging new alphabet value
newAlphabetValue = ALPHABET - 21
#creating the new string
newString = newString + chr(newAlphabetValue)
return newString
def main():
message = input("Enter text: ")
decrpytstr(message)
newString = decrpytstr(message)
print(newString)
input("Press ENTER to exit")
main()
https://www.asciitable.com/
正如我看到的 ascii table,b 98
的值将是 M 77
递减 21。我猜你必须分别处理大写和小写。
[已编辑]我不太明白你想达到什么目的,但是这个怎么样?
ALPHABET = "abcdefghijklmnopqrstuvwxyz"
def decrpytstr(message):
new_string = ""
for i in range(len(message)):
character = message[i]
new_character = ALPHABET[ALPHABET.find(character) - 21 % 26]
new_string = new_string + new_character
return new_string
def main():
message = input("Enter text: ")
decrpytstr(message)
newString = decrpytstr(message)
print(newString)
input("Press ENTER to exit")
main()
Enter text: xvznvm
caesar
Press ENTER to exit
有几个问题
- 您实际上并没有使用原来的字母表值
- 您没有考虑大写字符
- 我们需要环绕,这样当我们减去神奇的 21 步时,我们总能找到字母表中的东西
更正这些给我们这样的东西...
ALPHABET = "abcdefghijklmnopqrstuvwxyz0123456789."
def decrpytstr(message):
newString = ''
#loop to assign new alphabet value
for index in range(0, len(message)):
oneLetter = message[index]
#assinging new alphabet value
newAlphabetValue = (ALPHABET.find(oneLetter.lower()) - 21) % len(ALPHABET) + (ord('A') if oneLetter.isupper() else ord('a'))
#creating the new string
newString = newString + chr(newAlphabetValue)
return newString
def main():
message = input("Enter text: ")
decrpytstr(message)
newString = decrpytstr(message)
print(newString)
input("Press ENTER to exit")
main()