how does .isalpha and elif works ? (python)

661 Views Asked by At

I want to create a code that prints words in uppercase (after "G") from a sentence

# [] create words after "G"
# sample quote "Wheresoever you go, go with all your heart" ~ Confucius (551 BC - 479 BC)

# Sample output:

WHERESOEVER
YOU
WITH
YOUR
HEART

here is my code

q = input ("Quote : ")

word = ""
for a in q :
    if a.isalpha() == True :
        word = word + a
    elif word[0].lower() > "g" :
        print (word.upper())
        word = ""
    else :
        word = ""

it runs well until the last word of the sentence, it can't print the words although the first letter is after "G". Also, when it found punctuation, it stuck, and says

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-45-29b3e8e00230> in <module>()
      8     if a.isalpha() == True :
      9         word = word + a
---> 10     elif word[0].lower() > "g" :
     11         print (word.upper())
     12         word = ""

IndexError: string index out of range

I'm suspicious it has something whether with the .isalpha or the elif

I need to know how to fix it and where do I made mistakes

2

There are 2 best solutions below

2
On BEST ANSWER

If you have two characters in succession that are not letters (such as a comma followed by a space), then you will hit elif word[0].lower() > "g" when word is the empty string, so there will be no character at word[0]. That is what causes the exception.

Simplistically, you could avoid this by checking word isn't empty before you try and get word[0].

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

That at least should avoid the exception.


But I still have the last word issue, it doesn't print the last word although it started with alphabet greater than "G"

You only print when you hit a non-alphabetic character. So if you pass a word and then don't hit a non-alphabetic character, then the last word will not be printed.

To solve that, you can add a print after the end of your loop, in case there is a final word still to be printed.

for a in q:
    ... etc.

if word and word[0].lower() > 'g':
    print(word)
0
On

You will encounter two problems

1) Accessing word[0] while it is an empty string. Will give an error when you have successive non-alphabets. 2) Word is only printed when isalpha() is false. Here that simply means last word, "HEART" wont be printed.

To solve (1) you can check if word is an empty string before elif word[0].lower() > "g" : by adding if word=="". If it is an empty string we can skip the iteration, tackling the problem (1). To skip, you can use continue statement.What's continue? Detailed explanation here. Or just word = ""

To solve (2) you can do a simple trick by adding a whitespace after quote q+=" " {shortcut forq=q+""thus ensuring a non-alphabet as last character. Do it right after inputting quote.Or you can add

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

to the end. Now, my suggested final code will be,

q = input ("Quote : ")
q+=" "
word = ""
for a in q :
    if a.isalpha() == True :
        word += a
    elif word =="":
        continue
    elif word[0].lower() > "g" :
        print (word.upper())
        word = ""
    else :
        word = ""