Using while loops to count elements in a list

前端 未结 4 606
陌清茗
陌清茗 2020-12-22 07:23
places = [\"Jack\", \"John\", \"Sochi\"]
count=0
multi_word=0
place  = places[count]
while place != \"Sochi\" and count < len(places):
    if \' \' in place:
             


        
4条回答
  •  没有蜡笔的小新
    2020-12-22 07:34

    You can use the following while loop to check for the number of places before Sochi:

    places = ["Jack", "John", "Sochi"]
    count = 0
    multi_word = 0
    while count < len(places):
        place = places[count]
        if ' ' in place:
            multi_word += 1
        if place == "Sochi":
            break
        count += 1
    
    print('Number of cities before Sochi:', count)
    

    The break statement means you'll exit your while loop.

提交回复
热议问题