gpt4 book ai didi

python - 艰难地学习 Python 练习 20

转载 作者:太空宇宙 更新时间:2023-11-04 03:58:02 25 4
gpt4 key购买 nike

我不明白“line_count”的值是从哪里传递给这个变量的。如果你能给我解释一下,我将不胜感激!!输出打印每个连续的行。我了解它如何增加行号(1、2、3、4),但它实际上如何知道从何处获取数据以打印每个字符串让我感到困惑。

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)

最佳答案

line_count 是函数的一个参数;它通过调用函数时传递参数 的调用者获取其值。在这种情况下,参数是 current_line 全局变量的值。

关于python - 艰难地学习 Python 练习 20,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17253438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com