我不太清楚为什么StackOverflow不允许我在下面发布我的代码,所以我附加了一些链接来显示我所做的尝试。在输入完我的句子后,它会将任意数量的字符从1到25进行移动,效果很好。但是,我需要添加一个函数来反转编码,并将原来的句子打印出来。我不太清楚为什么Python会像在第二个链接中那样吐出解码后的句子。它应该有原来的句子。谢谢你的帮助!你知道吗
def encode( ch, shift):
lower = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWZ'
if ch not in lower:
return ch
newch = chr( ord(ch) + shift )
if newch not in lower:
newshift = ( ord(newch) - ord('z') - 1)
nwech = chr ( ord('a') + newshift )
return newch
def decoded(ch, shift):
lower = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWZ'
if ch not in lower:
return ch
newch = chr( ord(ch) + shift )
if newch not in lower:
newshift = ( ord(newch) - ord('z') - 1)
newch = chr ( ord('a') + newshift)
return newch
def main():
shift = int(input("Enter a number between 1 and 25:"))
sentence = input("Please enter a sentence:")
code = ''
decode = ''
for char in sentence:
code = code + encode (char, shift)
for char in code:
decode = decode + encode (char, shift)
print("Uncoded sentence: " + sentence)
print("Encoded sentence: " + code)
print("Decoded sentence: " + decode)
main()
Enter a number between 1 and 25:3
Please enter a sentence:i need help
Uncoded sentence: i need help
Encoded sentence: l qhhg khos
Decoded sentence: o tkkj nkrv
你不能调用decode方法。你调用encode方法两次。。。你知道吗
您的
decoded
函数似乎与encode
函数相同(在任何情况下都不会调用它)。更接近您需要的是:在
decoded
函数中,您应该在此处调用它来代替encode
:但现在你有另一个问题了。如果您的班次将您从字符的
ord
范围的末尾发送出去,会发生什么情况?你知道吗相关问题 更多 >
编程相关推荐