Help the cool guys
cool_people = "bert sally susan fred fiona"
pos = 0
start_pos = 0
while pos < len(cool_people):
if cool_people[pos] == " ":
print cool_people[start_pos:pos], "is cool"
start_pos = pos+1
pos = pos + 1
Not sure what the code does? Well here is an explanation!
line 1 - This creates the list of names, each separated by a space. This is important as we are looking for spaces to separate the names!
line 2 + 3 - This creates two variables and sets them to zero. Pos will keep track of where in the string we are and start_pos will keep track of the start of the name we are currently looking at.
line 4 - Will loop over every letter until we reach the end of the string. We can find the length by using the len function.
line 5 - cool_people[pos] will show the current letter. If this letter is a space then we MUST be at the first character after a name. As such we have just seen every letter of a name.
line 6 - cool_people[start_pos:pos] will return the name. start_pos will point to the start of the name and pos is the current position.
line 7 - This sets the start_pos to be the start of the next name. We add one otherwise we will start the name with a space (remember we are currently looking at a space at this point)
line 8 - Move onto the next letter until there are no more letters left.
In order to miss out the first letter of each name we simply change line 6 to say start_pos+1 when we get the name. Easy? Try it out!
Back to the problem armed with the answer!
Output: (clear)











