beginner help in python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: beginner help in python (/thread-31454.html) |
beginner help in python - aghapouria - Dec-12-2020 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? RE: beginner help in python - Larz60+ - Dec-13-2020 how about: def tokenize(text): for line in text: line = line.strip().split() print(line) tokenize(['apple pie'])
RE: beginner help in python - bowlofred - Dec-13-2020 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.
RE: beginner help in python - ndc85430 - Dec-13-2020 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 ?
RE: beginner help in python - aghapouria - Dec-13-2020 (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: Good exercise in using while loops. RE: beginner help in python - aghapouria - Dec-13-2020 (Dec-13-2020, 07:08 AM)bowlofred Wrote: And for your original code: 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? RE: beginner help in python - bowlofred - Dec-13-2020 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. RE: beginner help in python - aghapouria - Dec-13-2020 (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 ']) |