TypeError:在while循环中打印字典时,'str'对象不可调用

1 投票
2 回答
1459 浏览
提问于 2025-04-18 01:19

我在写一个字谜游戏(也叫替换字符的游戏)程序时遇到了问题,系统一直提示我“'str'对象不可调用”。这是我的代码(只包含与这个字典相关的while循环部分):

used_pairings = {}

while.....:
    used_pairings[guesschar] = guessletter
    print = ("At this point you can decide whether to delete a character-letter pairing.")
    used_pairings_choice = input("Type 1 to delete a pairing or ENTER to carry on: ")
    if used_pairings_choice == "1":
        print ("These are your pairings so far:")
        print (used_pairings)
    else:
        print ("Continuing program.")

这是我收到的错误信息:

"Codeword.py", line 79, in <module>
    print ("These are your pairings so far:")
TypeError: 'str' object not callable

我完全搞不懂这是什么意思,为什么会出现这个错误信息,所以任何帮助都非常感谢!提前谢谢大家!

2 个回答

1

问题在于,print 这个变量的声明。

print = ("At this point you can decide whether to delete a character-letter pairing.")

在这个声明之后,Python 的 print 函数就变成了一个名为 print 的变量,类型是 str(字符串)。

请更改变量的名称,然后再试试你的代码。

5

如果你在使用 Python 3 及以上版本,print 是一个函数。当你写 print = ("在这个时候你可以决定是否删除一个字符和字母的配对。") 时,其实是把内置的 print 函数给覆盖成了一个字符串。所以,只要把那行代码中的 = 去掉就可以解决这个问题了。

撰写回答