【问题标题】:String non callable in python 3python 3中不可调用的字符串
【发布时间】:2013-01-09 22:10:44
【问题描述】:

我在做 Learn Python the Hard Way(第 2 版,LPTHW)的第 16 次练习时遇到了一个奇怪的问题。
我首先输入了代码,复制了它,当我在控制台上执行脚本(使用 python ex16.py test.txt)时,会出现相同的消息:

File "ex16.py", line 19, in <module>  
line1 = input("line 1: ")  
TypeError: 'str' object is not callable    

代码是:

from sys import argv

script, filename = argv

print("We're going to erase %r." % filename)
print("If you don't want that, hit CTRL-C (^C).")
print("If you do want that, hit RETURN.")

input = ("?")

print("Opening the file...")
target = open(filename, 'w')

print("Truncating the file. Goodbye!")
target.truncate()

print("Now I'm going to ask you for three lines.")

line1 = input("line 1: ")
line2 = input("line 2: ")
line3 = input("line 3: ")

print("I'm going to write these to the file.")

target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")

print("And finally, we close it.")
target.close()  

这是因为 LPTHW 是为 Python 2.7 制作的,而我使用的是 Python 3.3 吗?

【问题讨论】:

    标签: string python-3.x


    【解决方案1】:

    你在这里隐藏了内置的 input 函数:

    input = ("?")
    

    等号分配给名为input 的变量,它隐藏了内置的input() 函数。删除等号,您的代码将起作用:

    input("?")
    

    【讨论】:

    • 非常感谢!我的错误来自 = 符号,它不应该在这里。
    【解决方案2】:
    input = ("?")
    

    注释掉上面的内容并重试脚本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-12
      • 1970-01-01
      • 1970-01-01
      • 2019-09-19
      • 2019-05-13
      • 2013-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多