【发布时间】:2021-08-06 18:45:56
【问题描述】:
运行此程序时,我收到错误“TypeError:'str' object is not callable”。有什么想法吗?
def caesar_encrypt(text,num):
i = ""
for i in text:
if not i.isalpha():
i += i
elif i.isupper():
i += i(((ord(i) + num - 65) %26) + 65)
else:
i += i(((ord(i) + num - 97) %26) + 97)
return i
s=input("please enter the message: ")
step=int(input("now enter the step "))
encrypted_msg=caesar_encrypt(s, num)
print(encrypted_msg)
【问题讨论】:
-
encrypted_msg=caesar_encrypt(s, num)->encrypted_msg=caesar_encrypt(s, step) -
欢迎来到 SO!
i( ... bunch of other stuff ... )应该如何工作?错误很明显:您正在调用像"x"()这样的字符串,它最低限度地重现了问题。i是一个字符串。看起来你有一个变量别名问题,循环变量覆盖了外部i。仅将i用于整数和索引,而不用于字符串或元素。你想完成什么? -
总是将完整的错误消息(从单词“Traceback”开始)作为文本(不是截图,不是链接到外部门户)有问题(不是评论)。还有其他有用的信息。
-
我认为您应该简单地删除
i在i += i(...)中()之前的i += i(...) -
您是否也必须能够解码编码字符串?