您将实现一个移位密码。这种类型的代码将每个字母移位一定的数量。
说明:完成编码方法,使其返回编码后的消息。完成解码方法,以便它返回解码后的消息。在主要方法中-提示用户输入消息。-提示用户输入班次金额。-打印消息。-呼叫编码与消息和移位量。-存储从编码返回的编码消息。-打印编码后的信息。-将编码后的消息和移位量发送到解码方法。-打印解码后的消息。
提示:chr(ord('A')+ 5)将给您'F',而chr(ord('F')-5)将给您'A'。您需要检查并调整班次将您移出字母结尾或开头的时间。例如,您将如何处理'B'-14?检查大写或小写字母的最简单方法是使用“ in”。请参考课堂上的示范。请记住,您可以将字母与大于和小于进行比较。 'A'>'B'将为False。您可能会假设没有标点符号(大多数代码都省略了标点符号,以避免提供有关已编码消息的线索)。您不必修改大小写。确保保留空间。 (尽管真正的密码不会)您可以使用该网站检查编码是否正常。 https://cryptii.com/pipes/caesar-cipher
样本输入/输出:输入消息:Hello World输入班次:10你好,世界罗夫维·吉布恩世界你好
我的代码
def encode(s, shiftamount):
for x in range(0, s-1):
if s[x] in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
if chr(ord(x) + shiftamount) > Z :
print(chr(ord(s[x])) + (shiftamount - 25),end="")
if chr(ord(x) + shiftamount) <= Z :
print(chr(ord(s[x])) + shiftamount,end="")
if s[x] in "abcdefghijklmnopqrstuvwxyz":
if chr(ord(x) + shiftamount) > Z :
print(chr(ord(s[x])) + (shiftamount - 25) ,end="")
if chr(ord(x) + shiftamount) <= Z :
print(chr(ord(s[x])) + shiftamount,end="")
print(chr(ord(s[x]) + shiftamount,end="")
#def decode(s, shiftamount):
def main():
s = input("Enter the message:")
shiftamount = input("Enter the shift")
print(s)
encode()
decode()
########################################################################
### Do not modify anything below here ###
########################################################################
if __name__ == '__main__':
main()
我认为此代码可以为您提供帮助
def encode(s, shiftamount):
for x in range(0, len(s)):
if s[x] in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
if chr(ord(x) + shiftamount) > Z :
print(chr(ord(s[x])) + (shiftamount - 25),end="")
if chr(ord(x) + shiftamount) <= Z :
print(chr(ord(s[x])) + shiftamount,end="")
if s[x] in "abcdefghijklmnopqrstuvwxyz":
if chr(ord(x) + shiftamount) > Z :
print(chr(ord(s[x])) + (shiftamount - 25) ,end="")
if chr(ord(x) + shiftamount) <= Z :
print(chr(ord(s[x])) + shiftamount,end="")
print(chr(ord(s[x]) + shiftamount,end="")
def main():
s = input("Enter the message:")
shiftamount = int(input("Enter the shift"))
print(s)
encode(s, shiftamount)