即使我还没有字符串,也会出现“不能调用字符串对象”的错误。 ```python my_string = 'Hello, World!' print(str(my_string)) ```

时间:2021-08-12 04:05:46

标签: python

我对 Python 非常新手。我正在尝试编写一个简单的加密程序。这是我的代码:

import random
z = input("请输入要加密的文本!")
c = random.randint(2, 26) #随机数字 2-26 之间
if c == 2:
   x = "b"

elif c == 3:
   x = "c"

elif c == 4:
   x = "d"

elif c == 5:
   x = "e"

elif c == 6:
   x = "f"

elif c == 7:
   x = "g"

elif c == 8:
   x = "h"

elif c == 9:
   x = "i"

elif c == 10:
   x = "j"

elif c == 11:
   x = "k"

elif c == 12:
   x = "l"

elif c == 13:
   x = "m"

elif c == 14:
   x = "n"

elif c == 15:
   x = "o"

elif c == 16:
   x = "p"

elif c == 17:
   x = "q"

elif c == 18:
   x = "r"

elif c == 19:
   x = "s"

elif c == 20:
   x = "t"

elif c == 21:
   x = "u"

elif c == 22:
   x = "v"

elif c == 23:
   x = "w"

elif c == 24:
   x = "x"

elif c == 25:
   x = "y"

elif c == 26:
   x = "z"

d = "我们建议..."
print("然后输入一些加密字符")
print(d + x)
before_b = input(":")

if before_b == "a":
  print("你是认真的吗?")
  b1 = "a"
  b2 = "b"
  b3 = "c"
  b4 = "d"
  b5 = "e"
  b6 = "f"
  b7 = "g"
  b8 = "h"
  b9 = "i"
  b10 = "j"
  b11 = "k"
  b12 = "l"
  b13 = "m"
  b14 = "n"
  b15 = "o"
  b16 = "p"
  b17 = "q"
  b18 = "r"
  b19 = "s"
  b20 = "t"
  b21 = "u"
  b22 = "v"
  b23 = "w"
  b24 = "x"
  b25 = "y"
  b26 = "z"

z = (z.replace("a", b1))
z = (z.replace("b", b2))
z = (z.replace("c", b3))
z = (z.replace("d", b4))
z = (z.replace("e", b5))
z = (z.replace("f", b6))
z = (z.replace("g", b7))
z = (z.replace("h", b8))
z = (z.replace("i", b9))
z = (z.replace("j", b10))
z = (z.replace("k", b11))
z = (z.replace("l", b12))
z = (z.replace("m", b13))
z = (z.replace("n", b14))
z = (z.replace("o", b15))
z = (z.replace("p", b16))
z = (z.replace("q", b17))
z = (z.replace("r", b18))
z = (z.replace("s", b19))
z = (z.replace("t", b20))
z = (z.replace("u", b21))
z = (z.replace("v", b22))
z = (z.replace("w", b23))
z = (z.replace("x", b24))
z = (z.replace("y", b25))
z = (z.replace("z", b26))

e = "文本加密完成!:"
f = "加密字符:"
print(e + z)
print(f + before_b)

(不要问为什么它如此冗长,我对此一无所知:p)然后谷歌 Colab 向我抛出了这个错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-29-91cda9e0e486> in <module>()
    114
    115 d = "我们建议..."
--> 116 print("然后输入一些加密字符")
    117 print(d + x)
    118 before_b = input(":")

TypeError: 'str' object is not callable

有人可以解释为什么它会在打印方面调用错误,并且如何修复它吗?

编辑#1:PyCharm 可以运行,但 Colab 无法运行。

2 个答案:

答案 0 :(得分:1)

TypeError: 'str'对象不可调用。

当将 print 赋值给变量时,如下所示:

>>> print = "hello"
>>> print("Hello")
Traceback (最近的一次调用):
  文件 "<stdin>", 线 1, 在<模块>中
TypeError: 'str'对象不可调用。

请确保您没有犯这个错误。

答案 1 :(得分:0)

当您尝试将字符串作为函数调用时,会引发此错误。要解决此错误,请确保不使用“str”作为变量。