0

This is the question I was given to solve:

Create a program inputs a phrase (like a famous quotation) and prints all of the words that start with h-z.

I solved the problem, but the first two methods didn't work and I wanted to know why:

#1 string index out of range

quote = input("enter a 1 sentence quote, non-alpha separate words: ")
word = ""

for character in quote:
    if character.isalpha():
        word += character.upper()
    else:
        if word[0].lower() >= "h":
            print(word)
            word = ""
        else: 
            word = ""

I get the IndexError: string index out of range message for any words after "g". Shouldn't the else statement catch it? I don't get why it doesn't, because if I remove the brackets [] from word[0], it works.

#2: last word not printing

quote = input("enter a 1 sentence quote, non-alpha separate words: ")
word = ""

for character in quote:
    if character.isalpha():
        word += character.upper()
    else:
        if word.lower() >= "h":
            print(word)
            word = ""
        else: 
            word = ""

In this example, it works to a degree. It eliminates any words before 'h' and prints words after 'h', but for some reason doesn't print the last word. It doesn't matter what quote i use, it doesn't print the last word even if it's after 'h'. Why is that?

2 Answers 2

2

You're calling on word[0]. This accesses the first element of the iterable string word. If word is empty (that is, word == ""), there is no "first element" to access; thus you get an IndexError. If a "word" starts with a non-alphabetic character (e.g. a number or a dash), then this will happen.

The second error you're having, with your second code snippet leaving off the last word, is because of the approach you're using for this problem. It looks like you're trying to walk through the sentence you're given, character by character, and decide whether to print a word after having read through it (which you know because you hit a space character. But this leads to the issue with your second approach, which is that it doesn't print the last string. That's because the last character in your sentence isn't a space - it's just the last letter in the last word. So, your else loop is never executed.

I'd recommend using an entirely different approach, using the method string.split(). This method is built-in to python and will transform one string into a list of smaller strings, split across the character/substring you specify. So if I do

quote = "Hello this is a sentence"
words = quote.split(' ')
print(words)

you'll end up seeing this:

['Hello', 'this', 'is', 'a', 'sentence']

A couple of things to keep in mind on your next approach to this problem:

  1. You need to account for empty words (like if I have two spaces in a row for some reason), and make sure they don't break the script.
  2. You need to account for non-alphanumeric characters like numbers and dashes. You can either ignore them or handle them differently, but you have to have something in place.
  3. You need to make sure that you handle the last word at some point, even if the sentence doesn't end in a space character.

Good luck!

Sign up to request clarification or add additional context in comments.

1 Comment

this was so helpful, especially the index error issue. Thank you so much for the response! I was given specific methods to solve the problem but I've just tried solving the question with the split function :) hope you have a great day
-1

Instead of what you're doing, you can Iterate over each word in the string and count how many of them begin in those letters. Read about the function str.split(), in the parameter you enter the divider, in this case ' ' since you want to count the words, and that returns a list of strings. Iterate over that in the loop and it should work.

1 Comment

thank you for your response! I was given specific methods to solve the problem but I just followed your advice and tried out the split method :) it works beautifully haha have a nice day <3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.