0
places = [ "Jack", "Jo hn", "Sochi", "Manan", "Mayank"]
count=0
multi_word=0
place  = places[count]
while place != "Sochi" :
    if ' ' in place:
        multi_word += 1

    count += 1
    place = places[count]

print ('Number of cities before Sochi:', count)
print ('Number of multiple names cities before Sochi:', multi_word)

This is my code I don't understand what this line (place = places[count]) does, nor do I understand why I need it twice.

Does it make the program start at Jack, then add 1 one once it reaches the next element of the list (Jo hn) and add one more and stop after Sochi, or does it add one at the first element of the list, then stop once it reaches Sochi.

5
  • Do you really need while loop? Commented Oct 14, 2013 at 12:05
  • To access items of the list you need places[count], but this can be done using a for-loop too. Commented Oct 14, 2013 at 12:06
  • this is a different question Commented Oct 14, 2013 at 12:08
  • 1
    You might find this useful: pythontutor.com/visualize.html Commented Oct 14, 2013 at 12:10
  • @MrE Awesome website! Should be very good for beginners to understand what python does internally. Commented Oct 14, 2013 at 12:19

1 Answer 1

0

Your algorithm can be expressed as follow:

places = [ "Jack", "Jo hn", "Sochi", "Manan", "Mayank"]
#Set the number of multiword to 0
multi_word=0
#Take the first element from the list as place
count=0
place  = places[count]
#If place is "Sochi" we're done
while place != "Sochi" :
    #Check if place is a multiword
    if ' ' in place:
        multi_word += 1
    #Move to the next element
    count += 1
    place = places[count]

The first time count is equal to 0 so that line basically takes the first element then, within the loop, count is incremented and the next element is taken from the list the iteration stops when place is equal to "Sochi", which means that "Sochi" and any element that follows won't be checked.

This is really confusing and badly written. Here's an easier version

place_list = [ "Jack", "Jo hn", "Sochi", "Manan", "Mayank"]
multi_word_count = 0
place_index = 0
while True:
    current_place = place_list[index]
    if current_place == "Sochi":
        break
    if ' ' in current_place:
        multi_word_count += 1
    place_index += 1

Now here's an even better solution

place_list = [ "Jack", "Jo hn", "Sochi", "Manan", "Mayank"]
multi_word_count = 0

for place in place_list:
    if place == "Sochi":
        break
    if ' ' in current_place:
        multi_word_count += 1     
Sign up to request clarification or add additional context in comments.

3 Comments

place_index appears to have disappeared in the last example...
@JonClements yep, cause it is not needed at all :) If you want the number of elements before "Sochi", just do a place_list.index("Sochi")
ahh... okay - thought you were trying to do everything in a single pass is all.. so I suppose you could do sum(' ' in place for place in itertools.takewhile('Sochi'.__ne__, place_list))

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.