使用raw_input“TypeError:'str'对象不可调用”

时间:2013-11-01 04:54:26

标签: python python-2.7

我正在从Zed Shaw的“学习Python艰难的方式”练习,而练习16似乎似乎不想工作:

#!/usr/bin/python
# -*- coding: utf-8 -*-

# reading and writing files

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 "Otherwise, press [ENTER] to proceed."

raw_input = ("?")

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

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

print "Enter three lines of input...."

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

print "Writing to the file...."

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

print "Closing the file...."
target.close()

# end program

无论出于何种原因,每次运行它都会返回:

mark@mark-KC880AA-ABA-m9150f:/media/KINGSTON/cis-115-09/LPTHW$ python ex16.py ex15Sample.txt
We're going to erase 'ex15Sample.txt'.
If you don't want that, hit CTRL-C (^C).
Otherwise, press [ENTER] to proceed.
Opening the file....
Truncating the file....
Enter three lines of input....
Traceback (most recent call last):
  File "ex16.py", line 24, in <module>
    line1 = raw_input("line 1: ")
TypeError: 'str' object is not callable

无论我做什么,line1变量似乎都会造成麻烦。我已经待了一个多小时了。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

代码覆盖以下行中的raw_input函数:

raw_input = ("?")

删除该行。 将其替换为raw_input('?')


>>> raw_input('line 1:')
line 1:111
'111'
>>> raw_input = ('?')
>>> raw_input('line 1:')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable