how does .isalpha and elif works ? (python)











up vote
-3
down vote

favorite












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










share|improve this question




















  • 2




    What is the idea behind elif ord(word[0].lower()) > 103? Why do you print when this condition matches?
    – khelwood
    Nov 19 at 15:45












  • That bit would be a lot clearer if it said elif word[0].lower() > 'g' rather than have the magic number 103 with no explanation in your code.
    – khelwood
    Nov 19 at 16:05












  • If you get an exception from this code (and I did when I ran it), please include the details of the exception in your question.
    – khelwood
    Nov 19 at 16:06












  • thanks, I have edited my code. I didn't know I can do that. By exception, do you mean the kind of error I encountered? IndexError: string index out of range. I'm sorry, I just started coding
    – Amri Rasyidi
    Nov 19 at 16:15










  • An exception normally produces a stack trace, which is a message describing an error, and a list of lines that tell you where the error occurred. You should include the stack trace in your question, or even better, use it to figure out where the problem is in your code.
    – khelwood
    Nov 19 at 16:17

















up vote
-3
down vote

favorite












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










share|improve this question




















  • 2




    What is the idea behind elif ord(word[0].lower()) > 103? Why do you print when this condition matches?
    – khelwood
    Nov 19 at 15:45












  • That bit would be a lot clearer if it said elif word[0].lower() > 'g' rather than have the magic number 103 with no explanation in your code.
    – khelwood
    Nov 19 at 16:05












  • If you get an exception from this code (and I did when I ran it), please include the details of the exception in your question.
    – khelwood
    Nov 19 at 16:06












  • thanks, I have edited my code. I didn't know I can do that. By exception, do you mean the kind of error I encountered? IndexError: string index out of range. I'm sorry, I just started coding
    – Amri Rasyidi
    Nov 19 at 16:15










  • An exception normally produces a stack trace, which is a message describing an error, and a list of lines that tell you where the error occurred. You should include the stack trace in your question, or even better, use it to figure out where the problem is in your code.
    – khelwood
    Nov 19 at 16:17















up vote
-3
down vote

favorite









up vote
-3
down vote

favorite











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










share|improve this question















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







python edx






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 16:27

























asked Nov 19 at 15:42









Amri Rasyidi

83




83








  • 2




    What is the idea behind elif ord(word[0].lower()) > 103? Why do you print when this condition matches?
    – khelwood
    Nov 19 at 15:45












  • That bit would be a lot clearer if it said elif word[0].lower() > 'g' rather than have the magic number 103 with no explanation in your code.
    – khelwood
    Nov 19 at 16:05












  • If you get an exception from this code (and I did when I ran it), please include the details of the exception in your question.
    – khelwood
    Nov 19 at 16:06












  • thanks, I have edited my code. I didn't know I can do that. By exception, do you mean the kind of error I encountered? IndexError: string index out of range. I'm sorry, I just started coding
    – Amri Rasyidi
    Nov 19 at 16:15










  • An exception normally produces a stack trace, which is a message describing an error, and a list of lines that tell you where the error occurred. You should include the stack trace in your question, or even better, use it to figure out where the problem is in your code.
    – khelwood
    Nov 19 at 16:17
















  • 2




    What is the idea behind elif ord(word[0].lower()) > 103? Why do you print when this condition matches?
    – khelwood
    Nov 19 at 15:45












  • That bit would be a lot clearer if it said elif word[0].lower() > 'g' rather than have the magic number 103 with no explanation in your code.
    – khelwood
    Nov 19 at 16:05












  • If you get an exception from this code (and I did when I ran it), please include the details of the exception in your question.
    – khelwood
    Nov 19 at 16:06












  • thanks, I have edited my code. I didn't know I can do that. By exception, do you mean the kind of error I encountered? IndexError: string index out of range. I'm sorry, I just started coding
    – Amri Rasyidi
    Nov 19 at 16:15










  • An exception normally produces a stack trace, which is a message describing an error, and a list of lines that tell you where the error occurred. You should include the stack trace in your question, or even better, use it to figure out where the problem is in your code.
    – khelwood
    Nov 19 at 16:17










2




2




What is the idea behind elif ord(word[0].lower()) > 103? Why do you print when this condition matches?
– khelwood
Nov 19 at 15:45






What is the idea behind elif ord(word[0].lower()) > 103? Why do you print when this condition matches?
– khelwood
Nov 19 at 15:45














That bit would be a lot clearer if it said elif word[0].lower() > 'g' rather than have the magic number 103 with no explanation in your code.
– khelwood
Nov 19 at 16:05






That bit would be a lot clearer if it said elif word[0].lower() > 'g' rather than have the magic number 103 with no explanation in your code.
– khelwood
Nov 19 at 16:05














If you get an exception from this code (and I did when I ran it), please include the details of the exception in your question.
– khelwood
Nov 19 at 16:06






If you get an exception from this code (and I did when I ran it), please include the details of the exception in your question.
– khelwood
Nov 19 at 16:06














thanks, I have edited my code. I didn't know I can do that. By exception, do you mean the kind of error I encountered? IndexError: string index out of range. I'm sorry, I just started coding
– Amri Rasyidi
Nov 19 at 16:15




thanks, I have edited my code. I didn't know I can do that. By exception, do you mean the kind of error I encountered? IndexError: string index out of range. I'm sorry, I just started coding
– Amri Rasyidi
Nov 19 at 16:15












An exception normally produces a stack trace, which is a message describing an error, and a list of lines that tell you where the error occurred. You should include the stack trace in your question, or even better, use it to figure out where the problem is in your code.
– khelwood
Nov 19 at 16:17






An exception normally produces a stack trace, which is a message describing an error, and a list of lines that tell you where the error occurred. You should include the stack trace in your question, or even better, use it to figure out where the problem is in your code.
– khelwood
Nov 19 at 16:17














2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










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)





share|improve this answer























  • Thank you very much. But I still have the last word issue, it doesn't print the last word although it started with alphabet greater than "G"
    – Amri Rasyidi
    Nov 20 at 10:24










  • OK. I've amended my answer.
    – khelwood
    Nov 20 at 10:27


















up vote
0
down vote













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 = ""





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378129%2fhow-does-isalpha-and-elif-works-python%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    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)





    share|improve this answer























    • Thank you very much. But I still have the last word issue, it doesn't print the last word although it started with alphabet greater than "G"
      – Amri Rasyidi
      Nov 20 at 10:24










    • OK. I've amended my answer.
      – khelwood
      Nov 20 at 10:27















    up vote
    0
    down vote



    accepted










    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)





    share|improve this answer























    • Thank you very much. But I still have the last word issue, it doesn't print the last word although it started with alphabet greater than "G"
      – Amri Rasyidi
      Nov 20 at 10:24










    • OK. I've amended my answer.
      – khelwood
      Nov 20 at 10:27













    up vote
    0
    down vote



    accepted







    up vote
    0
    down vote



    accepted






    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)





    share|improve this 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)






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 20 at 10:27

























    answered Nov 19 at 21:56









    khelwood

    29.8k74062




    29.8k74062












    • Thank you very much. But I still have the last word issue, it doesn't print the last word although it started with alphabet greater than "G"
      – Amri Rasyidi
      Nov 20 at 10:24










    • OK. I've amended my answer.
      – khelwood
      Nov 20 at 10:27


















    • Thank you very much. But I still have the last word issue, it doesn't print the last word although it started with alphabet greater than "G"
      – Amri Rasyidi
      Nov 20 at 10:24










    • OK. I've amended my answer.
      – khelwood
      Nov 20 at 10:27
















    Thank you very much. But I still have the last word issue, it doesn't print the last word although it started with alphabet greater than "G"
    – Amri Rasyidi
    Nov 20 at 10:24




    Thank you very much. But I still have the last word issue, it doesn't print the last word although it started with alphabet greater than "G"
    – Amri Rasyidi
    Nov 20 at 10:24












    OK. I've amended my answer.
    – khelwood
    Nov 20 at 10:27




    OK. I've amended my answer.
    – khelwood
    Nov 20 at 10:27












    up vote
    0
    down vote













    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 = ""





    share|improve this answer

























      up vote
      0
      down vote













      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 = ""





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        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 = ""





        share|improve this answer












        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 = ""






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 7:36









        Vaishak N

        11




        11






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378129%2fhow-does-isalpha-and-elif-works-python%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?