早教吧作业答案频道 -->英语-->
pythonfor循环问题shoplist=['apple','mango','carrot','banana']print'ihave',len(shoplist)+1,'itemtobuy'print'theitemare:',shoplistprint('\nihavetobuyrice.')shoplist.append('rice')print'myshoplistisnow',shoplistprint('iwillsort
题目详情
python for循环问题
shoplist = ['apple','mango','carrot','banana']
print 'i have',len(shoplist)+1,'item to buy'
print 'the item are:',shoplist
print ('\n i have to buy rice .')
shoplist.append('rice')
print'my shoplist is now',shoplist
print('i will sort my shoplist')
shoplist.sort()
print'sorted list is ',shoplist
count = 0
sz=['first','second','third','fouth','fifth']
for item in shoplist:
print'the ',sz[count],' item i will buy is',shoplist[0]
print('i bought the'),shoplist[0]
del shoplist[0]
print('my shoplist is now,'),shoplist
count=count+1
为什么结果只能出来我要买的东西只显示到third,后面的mango和rice不会出来,仍然留在shoplist里面求大神解答
shoplist = ['apple','mango','carrot','banana']
print 'i have',len(shoplist)+1,'item to buy'
print 'the item are:',shoplist
print ('\n i have to buy rice .')
shoplist.append('rice')
print'my shoplist is now',shoplist
print('i will sort my shoplist')
shoplist.sort()
print'sorted list is ',shoplist
count = 0
sz=['first','second','third','fouth','fifth']
for item in shoplist:
print'the ',sz[count],' item i will buy is',shoplist[0]
print('i bought the'),shoplist[0]
del shoplist[0]
print('my shoplist is now,'),shoplist
count=count+1
为什么结果只能出来我要买的东西只显示到third,后面的mango和rice不会出来,仍然留在shoplist里面求大神解答
▼优质解答
答案和解析
你用for循环遍历列表的同时还使用了del 改变列表长度,这是忌讳.
下面是更改后的代码,你可以对比看看.
shoplist = ['apple', 'mango', 'carrot', 'banana']
print 'I have', len(shoplist) + 1, 'items to buy'
print 'They are:', shoplist
print ('\nI have to buy rice.')
shoplist.append('rice')
print 'My shoplist is now', shoplist
print('I will sort my shoplist')
shoplist.sort()
print 'Sorted list is', shoplist
count = 0
sz = ['first', 'second', 'third', 'fouth', 'fifth']
for item in shoplist:
print 'The', sz[count], 'item I will buy is', shoplist[count]
print 'I bought the', shoplist[count]
print('My shoplist is now'), shoplist[count+1:]
count += 1
主要修改了for循环里面的相关语句.
下面是更改后的代码,你可以对比看看.
shoplist = ['apple', 'mango', 'carrot', 'banana']
print 'I have', len(shoplist) + 1, 'items to buy'
print 'They are:', shoplist
print ('\nI have to buy rice.')
shoplist.append('rice')
print 'My shoplist is now', shoplist
print('I will sort my shoplist')
shoplist.sort()
print 'Sorted list is', shoplist
count = 0
sz = ['first', 'second', 'third', 'fouth', 'fifth']
for item in shoplist:
print 'The', sz[count], 'item I will buy is', shoplist[count]
print 'I bought the', shoplist[count]
print('My shoplist is now'), shoplist[count+1:]
count += 1
主要修改了for循环里面的相关语句.
看了pythonfor循环问题sh...的网友还看了以下:
一辆汽车以速度V1匀速行驶全程的2/3的路程,接着以V2=20Km/h走完剩下的路程,若全路程的平 2020-05-16 …
有关系模式A(C,T,H,R,S),根据语义有如下函数依赖集:F={C→T,(H,R) →C, (H 2020-05-23 …
关系模式R(C,T,H,R,S),有如下函数依赖集:F={C→T,(H,R)→C,(H,T)→R,( 2020-05-24 …
关系模式R(C,T,H,R,S),有如下函数依赖集: F={C→T,(H,R)→C,(H,T)→R, 2020-05-24 …
有关系模式A(A,B,H,R,S),根据语义有如下函数依赖集:F={A→B,(H,R)→A,(H,B 2020-05-24 …
指出下面各个过程中体系的△U,△H,△S,△A和△G何者为零1,理想气体的卡诺循环2,H2,O2在 2020-07-05 …
职中数学题,关于集合.@@急!1)已知集合A={m,a,t,h,s},B={e,n,g,l,i,s, 2020-11-10 …
英语:下1.将下联打乱的字母组成单词,并写出汉语意思1.a,e,h,g,c,n[]2.e,i,s,t 2020-12-10 …
计算1mol理想气体做一次卡诺循环过程的ΔU,ΔH,ΔS.可追分!如题,请帮个忙! 2020-12-23 …
飞机的高度为H以v1速度飞行扔下一炸弹与飞机水平距离为s的地面拦截系统同时以v2速度竖直向上发射一炮 2021-01-06 …