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
#2
One issue with my code is this WHILE statement will never get to the last word.
Reply
#3
I've split this from the OP: https://python-forum.io/Thread-help-with...4#pid50274

That user is probably done with the class so the folks on that thread don't need the notifications of new activity there. (I just replied with a link to this thread, so that any relevant folks do get one notification.)

@peterd1965, were you just making a comment or are you still looking for help? Can you reproduce your problem with 5-10 lines of runnable code along with its expected vs actual output?
Reply
#4
You can either:
  • Add space to the end of your string - and you will find the last word
  • Print form the position of start_of_word after the loop
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Forum Jump:

User Panel Messages

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