Reputation: 27
This code works and all but I am not 100% sure how it works as it is in a Python book I borrowed. I don't understand how the program checks if something is multiword. Also what do the starred lines mean
places= ["Home","In-n Out Burger", "John's house", "Santa Monica Pier", "Staples center", "LA Dodgers stadium", "Home"]
def placesCount(places):
multi_word = 0
count = 0
**while True:
place = places[count]**
if place == 'LA Dodgers stadium':
break
**if ' ' in place:**
multi_word += 1
count += 1
return count + 1, multi_word + 1
placesCount(places)
Upvotes: 0
Views: 58
Reputation: 174708
The method checks if a string in the list places
has a space, it considers that a multi-word.
If the list places
contains the string LA Dodgers stadium
, the method will return the position of the string, plus a count of how many multiple words were found before it.
Here's a hint: What happens when you pass in ['LA Dodgers stadium']
to the function? Does it return the correct numbers?
def placesCount(places):
multi_word = 0 # a count of how many multiple words were found
count = 0 # an initializer (not needed in Python)
while True: # start a while loop
place = places[count] # get the object from the places list
# at position count
if place == 'LA Dodgers stadium':
# quit the loop if the current place equals 'LA Dodgers stadium'
break
if ' ' in place:
# If the current string from the places list
# (which is stored pointed to by the name place)
# contains a space, add one to the value of multi_word
multi_word += 1
# Add one to count, so the loop will pick the next object from the list
count += 1
# return a tuple, the first is how many words in the list
# and the second item is how many multiple words (words with spaces)
return count + 1, multi_word + 1
Upvotes: 1