Topic hỏi bài đầu tiên
Em đang làm cái bài 20 của anh Đạt, em đang luyện nhưng sai vài cái. Mấy anh chị sửa giúp. Thanks.
Đây là bài gốc:
from sys import argv
script, input_file = argv
def print_all(f):
print f.read()
def rewind(f):
f.seek(0)
def print_a_line(line_count, f):
print line_count, f.readline()
current_file = open(input_file)
print "First let's print the whole file:\n"
print_all(current_file)
print "Now let's rewind, kind of like a tape."
rewind(current_file)
print "Let's print three lines:"
current_line = 1
print_a_line(current_line, current_file)
current_line = current_line + 1
print_a_line(current_line, current_file)
Còn đây là bài luyện tập em làm:
from sys import argv
script, file_test = argv
def print_file(X):
print x.read()
def rewind_file(x):
x.seek(0)
def read_file_line(x):
print x.readline()
current_file = open(file_test)
print "Now rewind your file:"
print_file(current_file)
print "Rewind your file:"
rewind_file(current_file)
print "Now, print your damn line :D"
read_file_line(current_file)
Nhìn cũng không khác mấy, nhưng bản của em bị sai
PS C:\Users\Kevin\python\ex20> python .\ex20_pra.py file_test.txt
This is your file:
Traceback (most recent call last):
File “.\ex20_pra.py”, line 16, in
print_file(current_file)
File “.\ex20_pra.py”, line 5, in print_file
print x.read()
NameError: global name ‘x’ is not defined
PS C:\Users\Kevin\python\ex20> python .\ex20_pra.py file_test.txt
This is your file:
Traceback (most recent call last):
File “.\ex20_pra.py”, line 16, in
print_file(current_file)
File “.\ex20_pra.py”, line 5, in print_file
print x.read()
NameError: global name ‘x’ is not defined