[英]Why is ROT 13 displaying g in spaces?
I am writing code that is based on ROT13 Algorithm but when I have the message as "ROT ALGORITHM" it displays as "EBGgNYTBEVGUZ".我正在编写基于 ROT13 算法的代码,但是当我将消息显示为“ROT ALGORITHM”时,它显示为“EBGgNYTBEVGUZ”。 I am unsure whether the 'g' is wrong since its meant to be a space between ROT and ALGORITHM?我不确定“g”是否是错误的,因为它是 ROT 和 ALGORITHM 之间的空格?
def rot13(message,shift):
result = ""
for i in range(len(message)):
char = message[i]
if (char.isupper()):
result += chr((ord(char) + shift-13) % 26 + 65)
else:
result += chr((ord(char) + shift-13) % 26 + 97)
return result
shift = 13
message = "ROT ALGORITHM"
print("Shift:", shift)
print(message)
print(rot13(message,shift))
From ROT13 spec , only letters should be affected by the algorithm, here as space is not upper()
you go in the else section从ROT13 规范,只有字母应该受算法影响,这里因为空格不是upper()
你进入 else 部分
You may handle the 2 min usecases : lowercase and uppercase, and just use the alphabet to rotate您可以处理 2 分钟的用例:小写和大写,只需使用字母表来旋转
from string import ascii_lowercase, ascii_uppercase
def rot13(message, shift):
result = ""
for char in message:
if char in ascii_uppercase:
result += ascii_uppercase[(ascii_uppercase.index(char) + shift) % 26]
elif char in ascii_lowercase:
result += ascii_lowercase[(ascii_lowercase.index(char) + shift) % 26]
else:
result += char
return result
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.