0

I'm currently learning python from on Edx 'Python for the absolute beginner. i am to Create a program that inputs a phrase (like a famous quotation) and prints all of the words that start with h-z, i keep getting an error message on line, Here's the code,can someone please help out with a correction.

quote= input("enter a 1 sentence quote, non-alpha separate words:")
word=""
for char in quote:
    if char.isalpha() == True:
        word += char.upper()

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

        else:
            word=""
3
  • 1
    Have you used a debugger to see what changes are being made to word? Commented Aug 2, 2018 at 20:01
  • 3
    At the first iteration word is an empty string and does not have word[0]. Commented Aug 2, 2018 at 20:02
  • 2
    Initially, there is no index 0 of word. So if the first character is not alpha, you get this error. Commented Aug 2, 2018 at 20:02

2 Answers 2

1

Probably it is entering on ELSE clause on the first loop. Since word is still empty, there is no word[0].

But I really did not understand what you're exactly trying to do.

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

Comments

0

Try this:

quote=input("enter a 1 sentence quote, non-alpha separate words:")
[print(e) for e in quote.split(" ") if e[0].upper() in "HIJKLMNOPQRSTUVWXYZ"]

Comments

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.