Tình hình là em đang học Python trên LPTHW ở bài 20, dùng function để in ra nội dung file.
Đã in thành công, nhưng nó lại ra thế này, mọi người giúp em đưa nó về bình thường với ạ.
First let's print the whole file:
■I r e p r e s e n t f o r t h e f o r c e o f t r a n s c e n d e n t
W e w i l l c o n t r o l t h e n a t u r e .
I n c l u d i n g t h e * b e e p * m o t h e r n a t u r e .
Now let's rewind, kind of like a tape.
Let's print three lines:
■I r e p r e s e n t f o r t h e f o r c e o f t r a n s c e n d e n t
W e w i l l c o n t r o l t h e n a t u r e .
I n c l u d i n g t h e * b e e p * m o t h e r n a t u r e .
Đây là cái file test.txt
I represent for the force of transcendent.
We will control the nature.
Including the beep mother nature.
Đây là code (nhưng em nghĩ có lẽ cái này không phải lỗi ở code, đoán vậy)
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)
current_line = current_line + 1
print_a_line(current_line, current_file)
Cám ơn mọi người

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?