字符串在python 3中不可调用

时间:2013-01-09 22:10:45

标签: string python-3.x

我在做艰苦学习Python的第16次练习时遇到了一个奇怪的问题(第2版,LPTHW)。
我先输入代码,复制它,当我在控制台上执行脚本时(使用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?

2 个答案:

答案 0 :(得分:3)

你在这里隐藏了内置input功能:

input = ("?")

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

input("?")

答案 1 :(得分:0)

input = ("?")

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