我一直工作在Python中一個簡單的凱撒移位,但是當我嘗試運行它,它說:追加海峽到海峽
File "Ceaser Shift.py", line 36, in main
ciphertext += shift(letter,shift)
TypeError: 'str' object is not callable
我試圖找出它爲什麼這樣做,我可以添加到正常的IDLE環境中的字符串,並沒有看到任何在線關係,因爲我沒有在腳本中的任何地方重新定義str。 任何幫助將是偉大的!
我的代碼:
## Doesn't support changing shifts during computation, to do this either the database must be re-written or script restarted
import time, os, string
global selmemo
shiftmemo = {}
def shift(l,shift):
if l not in shiftmemo:
charset = list(string.ascii_lowercase)
place = charset.index(l.lower())
shiftplace = charset.index(shift.lower())
shiftmemo[l] = charset[(place+shiftplace)%25]
return shiftmemo[l]
def main():
shift = None
ciphertext = ""
print("--- Welcome ---")
print("--- Ceaser Shifter ---")
print("Commands: shift, encrypt, clear, print, quit")
choice = input(": ")
while choice != "quit":
if choice == "shift":
shift = input("Please enter a shift letter: ")
elif choice == "encrypt" and shift != None:
uparse = input("Enter your plaintext: ")
for letter in uparse:
if letter.lower() in string.ascii_lowercase:
ciphertext += shift(letter,shift)
else:
ciphertext += letter
elif choice == "clear":
shift = ""
ciphertext = ""
shiftmemo = {}
elif choice == "print":
print(ciphertext)
else:
pass
choice = input(": ")
main()
等等,不要錯過。非常感謝您的觀察!我想我缺乏睡眠 – Clement