Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
beginner help in python
#1
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?
snippsat write Dec-13-2020, 01:16 AM:
Added code tag in your post,look at BBCode on how to use.
Reply
#2
how about:

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

tokenize(['apple  pie'])
Output:
['apple', 'pie']
Reply
#3
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.
Reply
#4
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?
Reply
#5
(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.
Reply
#6
(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?
Reply
#7
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.
Reply
#8
(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 '])
Reply


Forum Jump:

User Panel Messages

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