我需要在Python的切片表示法上有一个很好的解释(引用是一个加号).
对我来说,这种符号需要一点点提升.
它看起来非常强大,但我还没有完全了解它.
我正在尝试在Python中创建一个简单的Caesar Cipher函数,它根据用户的输入移动字母,并在最后创建一个最终的新字符串.唯一的问题是最终的密文只显示最后一个移位的字符,而不是一个包含所有移位字符的整个字符串.
这是我的代码:
plainText = raw_input("What is your plaintext? ")
shift = int(raw_input("What is your shift? "))
def caesar(plainText, shift):
for ch in plainText:
if ch.isalpha():
stayInAlphabet = ord(ch) + shift
if stayInAlphabet > ord('z'):
stayInAlphabet -= 26
finalLetter = chr(stayInAlphabet)
cipherText = ""
cipherText += finalLetter
print "Your ciphertext is: ", cipherText
return cipherText
caesar(plainText, shift)
Run Code Online (Sandbox Code Playgroud)