代码如下:
text = input("What's your text: ")
shift = int(input("What's your shift: "))
def caesar_shift(text, shift):
cipher = ""
for i in text:
if i.isalpha():
stayIn = ord(i) + shift
if stayIn > ord('z'):
stayIn -= 26
lastLetter = chr(stayIn)
cipher += lastLetter
print("Your ciphertext is: ", cipher)
return cipher
caesar_shift(text, shift)
当我运行它时,例如,测试是hello world,而移位是1,我得到:
What's your text: hello world
What's your shift: 1
Your ciphertext is: i
Your ciphertext is: if
Your ciphertext is: ifm
Your ciphertext is: ifmm
Your ciphertext is: ifmmp
Your ciphertext is: ifmmpp
Your ciphertext is: ifmmppx
Your ciphertext is: ifmmppxp
Your ciphertext is: ifmmppxps
Your ciphertext is: ifmmppxpsm
Your ciphertext is: ifmmppxpsme
为什么会这样?我做错什么了,提前谢谢!你知道吗
要解决打印问题,您必须:
但你应该有
或者更好
你知道吗
但是你没有其他条款来解释这个假设。也就是说,当最后一个字母不是字母时,也要加上它。因此
ifmmpp
而不是ifmmp
表示hello
。你知道吗该位应更改为:
如果不希望每个循环打印一次结果,请将其移到循环外。你知道吗
相关问题 更多 >
编程相关推荐