Python Forum
[split] help with While method
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] help with While method
#1
It appears I'm taking the same class as "brawdhampshire".
We don't know ".join" yet.
"brawdhampshire" I believe they are asking us to loop through the string and parse out each word.
Since we don't know ".join" wink, wink. My thought is we need to go through the string and find instances of the space (" "). The spaces will become our markers and we need to grab word between the spaces.

Below is my overly verbose code for purpose of explaining:

#start program here
quote = "they stumble who run fast"

start_of_word = 0
space_index = quote.find(" ")
while space_index != -1:
    # code to print word (index slice start:space_index)
    print("First line of the while, the space_index = ", space_index)
    print("Lets get the word between index ",start_of_word, "and" ,space_index )
    
    print("Here is the word :" , quote[start_of_word:space_index])
    print ("\n")

    start_of_word = space_index +1
    print("Start now = :", start_of_word)
    
    space_index = quote.find(" ", start_of_word)
    print("Last line of while, space index now = ", space_index)
    start_of_word = space_index +1
    print("Start now = :", start_of_word)
    
    space_index = quote.find(" ", start_of_word)
    print("Last line of while, space index now = ", space_index)
This will output:

Output:
First line of the while, the space_index = 4 Lets get the word between index 0 and 4 Here is the word : they Start now = : 5 Last line of while, space index now = 12 First line of the while, the space_index = 12 Lets get the word between index 5 and 12 Here is the word : stumble Start now = : 13 Last line of while, space index now = 16 First line of the while, the space_index = 16 Lets get the word between index 13 and 16 Here is the word : who Start now = : 17 Last line of while, space index now = 20 First line of the while, the space_index = 20 Lets get the word between index 17 and 20 Here is the word : run Start now = : 21 Last line of while, space index now = -1
Reply


Messages In This Thread
[split] help with While method - by peterd1965 - Jun-14-2018, 12:56 PM
RE: [split] help with While method - by micseydel - Jun-20-2018, 05:40 PM
RE: [split] help with While method - by volcano63 - Jun-20-2018, 05:45 PM
RE: help with While method - by peterd1965 - Jun-20-2018, 12:26 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020