#-*-coding:gb2312-*-
f_1 = 'cats.txt'
f_2 = 'dogs.txt'
'''cats檔案內容為:tom jerry teddy
dog檔案內容為:
mitchel
rubin
glass
'''
try:
with open(f_1) as f_o1:
content_1 = f_o1.read()
except FileNotFoundError :
print("no such file")
else:
cats = content_1.split()
try:
with open(f_2) as f_o2:
content_2 = f_o2.read()
except FileNotFoundError :
print("no such file")
else:
dogs = content_2.split()
for dog in dogs:
print(dog)
print("\n")
'''為什么下面這行代碼不列印整個串列的所有元素'''
'''很顯然for回圈里面有一些我不懂的東西'''
for cat in cats:
print(cat+'\n')
print(cats)
current = cats.pop()
dogs.append(current)
print("\n")
for dog in dogs:
print(dog)
print("\n")
for cat in cats:
print(cat)
current = cats.pop()
dogs.append(current)
這個情況中我把for換成while就可以符合我預想的邏輯:將cats串列的所有元素移動到dogs中,但是使用for回圈就出現了問題。不明白for的作業原理。
uj5u.com熱心網友回復:
你既然用pop()彈出并洗掉元素,for回圈就不要用cats了,用for i in range(len(cats))轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/144903.html
