其中10-1是一个有多行英文的文本文件
我发现文件的内容只打印了一次
而 yes 出现了三次
这是为什么?
a=3
with open('10-1.txt') as file:
while a>0:
for line in file.readlines():
print(line.rstrip())
print('yes')
a=a-1
这是输出
其中10-1是一个有多行英文的文本文件
我发现文件的内容只打印了一次
而 yes 出现了三次
这是为什么?
a=3
with open('10-1.txt') as file:
while a>0:
for line in file.readlines():
print(line.rstrip())
print('yes')
a=a-1
本来应该是都打印三次的,但是file.readlines()类似于迭代器,一遍循环已经把里面的元素全部迭代完了,所以第二遍第三遍就不会再打印了,理解的话点击一下采纳谢谢