0

我正在做 Zed Shaw 的“Learn Python The Hard Way”中的练习,练习 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 变量似乎都会造成麻烦。我已经玩了一个多小时了。有什么建议吗?

4

1 回答 1

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
于 2013-11-01T04:55:07.160 回答