01/10/2018, 16:01

2 code khác nhau chỉ 1 dòng, tại sao code 1 in được còn code 2 không in được?

Cho e hỏi
Đoạn code 1 thì run đượ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): # in 1 dong, line_count ghi so dau dong
	print line_count, f.readline(), # readline khi chay tu dong co 1 khoang trong xuong hang neu k thich nhu the chi can them dau phay(,).
	
current_file = open(input_file)

print "First let's print the whole file:
"
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
i = current_line
while i < 4:
	i = current_line	
	print_a_line(current_line, current_file)
	current_line += 1

Đoạn code 2 thì không run đượ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:
"
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
i = current_line
while i < 4:
	i = current_line	
	print_a_line(current_line, current_file)
	i += 1

Chỉ khác ở chỗ curent_line và i tại sao đoạn 2 k in ra được

HK boy viết 18:16 ngày 01/10/2018

Code 1: Biến current_line thay đổi, biến i thay đổi theo biến current_line

Code 2: Biến i thay đổi, biến current_line không thay đổi.

Cả 2 code sẽ đều chạy được, chỉ là 1 code chạy đúng, 1 code chạy sai.

Bài liên quan
0