1

I'm a beginner programmer and as practice I want to make a code that would print every word in a sentence into a new line given that the word starts with h-z.

Here's the code I made:

random_phrase = "Wheresoever you go, go with all your heart"
word = ""
for ltr in random_phrase:
    if ltr.isalpha():
        word += ltr
    else:
        if word.lower()[0] > "g":
            print(word)
            word = ""
        else:
            word = ""

After it prints you, there's a line of empty space, then the index error occurs. What did I do wrong?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

5 Answers5

2

Try this to print every word of a sentence that starts with h-z:

import string
# make the sentence lower case
random_phrase = "Wheresoever you go, go with all your heart".lower()
# split the sentence with space as separator into a list
words = random_phrase.split(" ")
start = "h"
end = "z"
# string.ascii_lowercase will give the lowercase letters ‘abcdefghijklmnopqrstuvwxyz’
letters = string.ascii_lowercase
# take the range h-z
allowed = letters[letters.index(start):letters.index(end)+1]
# iterate through each word and check if it starts with (h-z)
for word in words:
    if any(word.startswith(x) for x in allowed):
        print(word)

Using regular expressions:

import re
random_phrase = "Wheresoever you go, go with all your heart"
words = [s for s in random_phrase.split() if re.match('^[h-z,H-Z]', s)]
for word in words:
    print(word)
this.srivastava
  • 1,045
  • 12
  • 22
1
  • In your original code, you have a , which is causing your code to break, since it is not an alphanumeric character, so it goes into the else loop, where your word is an empty string, and when you try to get the first element is a empty string, you get IndexError. Hence it is better to check for a non-whitespace character like if ltr != ' ': .

  • You can reset word = "" outside if condition

  • Your code doesn't check the last word in the string, which you can do after your for loop finishes

So your refactored code will look like

random_phrase = "Wheresoever you go go with all your heart"
word = ""
for ltr in random_phrase:
    #Check for non-whitespace character
    if ltr != ' ':
        word += ltr
    else:
        #Check if word starts with g
        if word.lower()[0] > "g":
            print(word)
        #Reset word variable
        word = ""

#Check the last word
if word.lower()[0] > "g":
    print(word)

The output will be

Wheresoever
you
with
your
heart

You can simplify your code a lot by splitting your string into words, by using string.split, iterating through the words, and then checking if the first character of each word lies in h-z

random_phrase = "Wheresoever you go, go with all your heart"

#Split string into words
words = random_phrase.split()

#Iterate through every word
for word in words:

    #If the first character of the word lies between h-z, print it
    if word.lower()[0] > "g":
        print(word)

Here your output will be

Wheresoever
you
with
your
heart
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
1

What did I do wrong?

When your process encounters the comma (not-alpha) after the word go it determines that go does not start with a letter greater than 'g' so it assigns an empty string to word (else; word = ''). The next item is a space which is not-alpha and it tries to compare the first letter of word which is an empty string (''[0] > 'g') so it causes an IndexError.

You can fix it by checking to see if word is an empty string before the comparison.
Change

if word.lower()[0] > "g":

to

if word and word.lower()[0] > "g":

An empty string evaluates to a boolean False:

>>> s = ''
>>> bool(s)
False
>>>

and is a boolean operation; x and y will evaluate to False if either term is False - x is evaluated first; if it is False, its value will be returned and y will not be evaluated. This is referred to as short circuit behavior.

>>> x = ''
>>> y = 'foo'
>>> x and y
''
>>> bool(x and y)
False
>>> if x and y:
...     print(True)
... else:
...     print(False)

False
>>> 

My fix for your conditional statement relies on the short-circuit behavior so that word[0]... won't be evaluated if word is an empty string. Your else clause could be written a little more explicit - something like:

....
    else:
        if not word:
            pass
        elif word.lower()[0] > "g":
            print(word)
        word = ""

but for some reason I didn't like how that looked.

wwii
  • 23,232
  • 7
  • 37
  • 77
  • This is great! Thank you. If I may ask, however, how does simply writing "word and" check if it is an empty string? – Jill and Jill May 03 '19 at 05:03
  • You can simpilfy your code a lot by splitting string into words, and checking for the condition! @JillandJill Check my answer if it makes sense to you! – Devesh Kumar Singh May 03 '19 at 05:15
0

I think the error occurs because of this chunk of the code:

    if word.lower()[0] > "g":
        print(word)
        word = ""
    else:
        word = ""

You're reseting the word variable into a blank. Indexing on a blank does not work (as you can see when you try executing this code):

word = ""
word[0]

IndexError: string index out of range
zero
  • 1,605
  • 3
  • 15
  • 24
0

Always go for simple solutions...

phrase = "Wheresoever you go, go with all your heart"
for word in (phrase.lower()).split():
   first_letter = word[0]
   if first_letter > "g":
      print(word)
sinG20
  • 143
  • 2
  • 15