Too much spaces in the output

hey, im learning from edx and we had a small program to do , my program works good exapt the output prints the words with too many spaces.

quote = input("write your quote here: ")
word = ""
for x in quote.lower():
    if x.isalpha() == True:
        word += x
    else:
        for y in word:
            if y.lower() >= 'h':
                print(word.upper())
                word = "" 
            else:
                word = ""
print(word.upper())

output:

write your quote here: Wheresoever you go, go with all your heart
WHERESOEVER








YOU






WITH









YOUR






HEART

how do I fix it?

Javascript has string method of trim() that removes whitespace at the beginning and end of word. Maybe look for something like that in in Python?

Python automatically puts the output with a newline. Very handy, but not in this case. So in python 3 you can specify which ending you want: print(’.’, end=’’), for example.

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums

i tried it but i combines all the word together and my goal is seperate words in seperate lines. i also tried the “\n” function but it didnt worked well.

I’m learning Python too so tried to debug your problem. I’ve found this… :slight_smile:

Your first for loop, for x in quote.lower():, goes through the loop to get the word from the sentence. Then, when a space is found, you go to the else criteria, for y in word:. This will now loop through the word. You are then comparing each letter of that word with the letter 'h' and if it is equal of more than it, then it will print the word but also set the word to be blank. This loop continues for each of the letters and prints the word each time (which you have set to blank after it’s printed it first time). So, the extra lines are all the cases where the letter in the word is >= 'h' after you have reset the word value to be blank.

So, for the word ‘Wheresoever’, after it printed ‘WHERESOVER’ but that was when ‘w’ (First letter) >= ‘h’. It then checked if ‘h’ >= ‘h’ which it is so printed “”. Then it checked ‘e’ >= ‘h’ which it isn’t so it didn’t print etc.


To debug it, I added print(y) before the if condition of the second loop, it was here that I noticed that the space appeared after the letters which were more than or equal to ‘h’ so I realised that it’s printing the word = "" each time.

Debug Code
quote = input("write your quote here: ")
word = ""
for x in quote.lower():
    if x.isalpha() == True:
        word += x
    else:
        for y in word:
            print(y)    #Added this extra print statement
            if y.lower() >= 'h':
                print(word.upper())
                word = "" 
            else:
                word = ""
print(word.upper())
Debug Code Output (For Input: "Wheresoever you go, go with all your heart")
w
WHERESOEVER
h

e
r

e
s

o

e
v

e
r

y
YOU
o

u

g
o

g
o

w
WITH
i

t

h

a
l

l

y
YOUR
o

u

r

HEART

I suggest modifying your code so that it doesn’t loop through each of the letters once the word has been determined. HINT: See if you can use the word to work out if it needs to be printed. If you get stuck, my modified code is in the spoiler below.

Altered code (Spoiler)
quote = input("write your quote here: ")
word = ""
for x in quote.lower():
    if x.isalpha() == True:
        word += x
    else:
        if word >= 'h':
            print(word.upper())
            word = ""
        else:
            word = ""
print(word.upper())