早教吧作业答案频道 -->英语-->
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...的网友还看了以下:
is 是否在任何情况下都能缩写成's 比如非she he that等代词而是名词如如 this a 2020-04-05 …
Are there apple?该为单数句It"s too big.改为复数句His hat is 2020-05-14 …
英语翻译2011年9月21日的,P17,下面是文章~Apple bites back with 4 2020-05-16 …
特殊疑问句改成陈述句(间接引语)Whose is this apple?Whose apple i 2020-05-16 …
英语的名字最后有一个s,物主关系怎么表示?英语的名字最后有一个s,比如一个人名字叫Hollis,那 2020-05-16 …
6.-Are(that)(apple-Yes,(it)are.6.-Are(that)(apple 2020-07-08 …
pythonfor循环问题shoplist=['apple','mango','carrot','b 2020-10-29 …
apple+pineapple+orange+pear+pear=20+++++grapes+app 2020-10-29 …
单项选择.1.It's()apple,not()pear.a.an;ab.an;thec.a;a2. 2020-10-29 …
求详细的解释一道编程题执行下列程序后,输出的第一行为,第二行为。#includeintfun(cha 2020-10-29 …