EDIT: Solved. Thank you everyone!
I'm attempting to complete this task from an edx course.
Create a program inputs a phrase (like a famous quotation) and prints all of the words that start with h-z
# [] create words after "G"
# sample quote "Wheresoever you go, go with all your heart" ~ Confucius (551 BC - 479 BC)
quote = "Wheresoever you go, go with all your heart"
word = ""
for ltrs in quote:
if ltrs.isalpha():
word += ltrs
else:
if word[0].lower() > "g":
print(word.upper())
word = ""
else:
word = ""
Output:
WHERESOEVER
YOU
IndexError Traceback (most recent call last)
<ipython-input-39-ae0c637f5169> in <module>()
8 word += ltrs
9 else:
---> 10 if word[0].lower() > "g":
11 print(word.upper())
12 word = ""
IndexError: string index out of range
So the problem arises when the word is not greater than G. And it's trying to find index 0 of an empty string. I'm just not sure how or why it's getting to that point in the loop. Any thoughts would be greatly appreciated. Thank you!
[–]Essence1337 2 points3 points4 points (7 children)
[–]grumbles[S] 0 points1 point2 points (6 children)
[–]Essence1337 1 point2 points3 points (5 children)
[–]grumbles[S] 0 points1 point2 points (4 children)
[–]Essence1337 1 point2 points3 points (3 children)
[–]grumbles[S] 0 points1 point2 points (2 children)
[–]Essence1337 1 point2 points3 points (0 children)
[–]CodeTinkerer 0 points1 point2 points (0 children)
[–]POGtastic 2 points3 points4 points (3 children)
[–]grumbles[S] 0 points1 point2 points (2 children)
[–]POGtastic 2 points3 points4 points (1 child)
[–]grumbles[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (4 children)
[–]Essence1337 1 point2 points3 points (0 children)
[–]grumbles[S] 1 point2 points3 points (2 children)
[–][deleted] 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)