#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    Separate syllables creating rules


    I'm a beginner in Python. So far I got this:

    Code:
    vowels = 'aeiouAEIUO'
    consonants= 'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ'
    a = input('Type a word: ')
    
    for i in range(len(a)):
        cont1 = 0
        cont2 = 0
        if a[i] in consonants:
            cont1 = i - 1
            cont2 = i + 1
            if a [cont1] in vowels and a [cont2] in vowels:
                    print('consonant between vowels')

    I want to separate syllables. I want to create a rule that when a consonant is between 2 vowels, the consonant has to join to the vowel on it's right and print the syllables separated by '-' montoya = mon-to-ya amor = a-mor clase = cla-se

    This code of course gives an error: IndexError: string index out of range. I don't know what that means.

    Thanks
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,635
    Rep Power
    1945
    When posting code you should use [code ] [ /code] tag to keep the indention/spaces.

    the error means that you try to access a value in the string array which doesn't exists.
    It occurs because you try to access a[cont2] when i is the index of the last letter (len[a]).
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by MrFujin
    When posting code you should use [code ] [ /code] tag to keep the indention/spaces.

    the error means that you try to access a value in the string array which doesn't exists.
    It occurs because you try to access a[cont2] when i is the index of the last letter (len[a]).
    Thanks, but I don't understand. Like I said, I'm just a beginner. How can I fix it?
  6. #4
  7. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,635
    Rep Power
    1945
    the variable i should only go to the second-last letter , which is range(len(a)-1)
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    3
    Just to add to what MrFujin said, you can trap errors like the with try {} except {}. So you put the code that produces the error in the try {...code goes here...} block, and what to do if it doesn't work (like if the word is only 1 syllable) in the except {...other code goes here...} block.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    487
    Rep Power
    33
    Please be aware that "Thanks, but I don't understand. Like I said, I'm just a beginner. How can I fix it?" is no reason to help you because you haven't done anything yourself. What did you get when you searched for the error and what worked and what did not.

    On the first pass i-1 yields a negative number. Take a look at this page on strings, especially the "IndexError: string index out of range" portion.
    Code:
    cont1 = i - 1
    Code:
            cont1 = i - 1
            cont2 = i + 1
            if cont1 >= 0 and cont2 < len(a):

    Comments on this post

    • MrFujin agrees : For spotting the out of index range can happen in both way.
    Last edited by dwblas; December 26th, 2013 at 11:05 AM.
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,851
    Rep Power
    481
    Let's help brogad. brogad might also be new to programming and need help developing algorithms.

    So far your program looks for a consonant between two vowels. A consonant cannot be between two vowels when at either end of the word. We'll change the range of your loop to examine only possible candidates.
    Code:
    for i in range(1, len(a)-1):
    Doing so will also correct the indexing out-of-bounds elements of the word trouble. When i has value len(a)-1 then the index i+1 is beyond the end of the array causing index error. And at the other extreme, when i was 0 the i-1 index is -1. -1 is a valid index for non-empty strings with meaning "the last item". It did not cause an error. a[-1] did fetch an unintended character.
    Last edited by b49P23TIvg; December 26th, 2013 at 11:47 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0
    Thank you very much. It works.

IMN logo majestic logo threadwatch logo seochat tools logo