Python Forum

Full Version: beginner help in python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

I am trying to learn python and I am working with some problems in the book, I am trying to construct a function that reads the lines of a string input and prints out the words. I wish to remove the spacebars with a while loop. Here is the code for simply displaying the string:
def tokenize(lines):
    
    for line in lines:
        start = 0
        while start < len(line):
            print(line[start])
            start = start +1
    

tokenize(['apple  pie'])
-----------------------------------------------------------------------
And here is my attempt in trying to remove the spacebars with a while loop.

def tokenize(lines):
    
    for line in lines:
        start = 0
        while start < len(line):
            while line.isspace == False:
                print(line[start])
            start = start +1
    

tokenize(['apple  pie'])
Why doesn't my whileloop work? How do i change it so i don't have the spacebars in the final outprint?
how about:

def tokenize(text):
    for line in text:
        line = line.strip().split()
        print(line)

tokenize(['apple  pie'])
Output:
['apple', 'pie']
And for your original code:

* You're comparing line.isspace, not line.isspace()
* line.isspace() is only true if the entire line is spaces.
* You're updating start, but your inner while doesn't change the comparison.

I suspect you intended the inner while to instead be if line[start].isspace() == False:

But the split() version is better.
Why do you need to write your own function to do this, when there's a perfectly good one in the standard library: str.splitlines?
(Dec-13-2020, 07:22 AM)ndc85430 Wrote: [ -> ]Why do you need to write your own function to do this, when there's a perfectly good one in the standard library: str.splitlines?

Good exercise in using while loops.
(Dec-13-2020, 07:08 AM)bowlofred Wrote: [ -> ]And for your original code:

* You're comparing line.isspace, not line.isspace()
* line.isspace() is only true if the entire line is spaces.
* You're updating start, but your inner while doesn't change the comparison.

I suspect you intended the inner while to instead be if line[start].isspace() == False:

But the split() version is better.

You are absolutely right, i forgot the [start]-part and now it is working. However now i get a "string index out of range error", any idea why?
You'll need to show the code. When I change the code to have the line above, it doesn't do that. Show what you're running now.
(Dec-13-2020, 04:57 PM)bowlofred Wrote: [ -> ]You'll need to show the code. When I change the code to have the line above, it doesn't do that. Show what you're running now.

It's working now, i don't know what i changed i just restarted the program. Here is the final code, btw thank you everyone for the help.
def tokenize(lines):
words = []
for line in lines:
start = 0
while start < len(line):
while line[start].isspace() == False:
print(line[start])
start = start +1
start = start+1
return words

tokenize([' apple pie ',' is in the making of '])