关于python的for循环
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于python的for循环相关的知识,希望对你有一定的参考价值。
ten_thing = "Apple Orange Crows Telephone Light Sugar"
print("Wait there are not 10 things in that list. Let's fix that.")
stuff = ten_thing.split(' ')
more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "boy"]
while len(stuff) != 10:
next_one = more_stuff.pop()
print("Adding: ", next_one)
stuff.append(next_one)
print(f"There are len(stuff) items now.")
各位大神吗,上面的那段while的循环如果用for语句写应该怎么写?
怎么复制老师把缩进吃掉。。。
print("Wait there are not 10 things in that list. Let's fix that.")
stuff = ten_thing.split(' ')
more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "boy"]
def test():
n=0
for i in stuff:
n=n+1
if n!=10:
next_one = more_stuff.pop()
print("Adding: ", next_one)
stuff.append(next_one)
print(f"There are len(stuff) items now.")
return test()
test()
非要用个for的话··我只能想到这个办法了···
参考技术A python里边while循环,除了简单的i < n这一类的可以用for in range(n)替代,其他的是没法用for替换的,或者说很难替换,它和c语言一类的里边的for是两回事以上是关于关于python的for循环的主要内容,如果未能解决你的问题,请参考以下文章