Thread: Please help!

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

    Join Date
    Nov 2017
    Posts
    3
    Rep Power
    0

    Please help!


    I want to write this small program in Python:
    Write a while loop that starts at the last character in the string and works its way backwards to the first character in the string, printing each letter on a separate line, except backwards.
    I tried this:

    index = 0
    fruit="banana"
    while index < len(fruit):
    letter = fruit[index-1]
    print(letter)
    index = index-1

    it works but with an error:

    a
    n
    a
    n
    a
    b
    Traceback (most recent call last):
    File "/Users/bicharaetienne/Desktop/while.py", line 6, in <module>
    letter = fruit[index-1]
    IndexError: string index out of range

    How can I make it work perfectly?

    Thanks!
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,946
    Rep Power
    509
    We notice an index error. Let's find the value of the causal index. The index variable starts at 0, and decreases, and because python seamlessly promotes integers into an internal extended precision representation, index will always be less than len('banana').
    Code:
    '''
        $ python3 p.py
        trying "banana"[-1]
        a
        trying "banana"[-2]
        n
        trying "banana"[-3]
        a
        trying "banana"[-4]
        n
        trying "banana"[-5]
        a
        trying "banana"[-6]
        b
        trying "banana"[-7]
        Traceback (most recent call last):
          File "p.py", line 26, in <module>
            letter = fruit[index-1]
        IndexError: string index out of range
    '''
    
    index = 0
    fruit="banana"
    while index < len(fruit):
        print('trying "{}"[{}]'.format(fruit, index - 1))  ################ inserted this diagnostic
        letter = fruit[index-1]
        print(letter)
        index = index-1
    Last edited by b49P23TIvg; November 25th, 2017 at 03:23 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2017
    Posts
    3
    Rep Power
    0
    Originally Posted by b49P23TIvg
    We notice an index error. Let's find the value of the causal index. The index variable starts at 0, and decreases, and because python seamlessly promotes integers into an internal extended precision representation, index will always be less than 'banana'.
    Code:
    '''
        $ python3 p.py
        trying fruit[-1]
        a
        trying fruit[-2]
        n
        trying fruit[-3]
        a
        trying fruit[-4]
        n
        trying fruit[-5]
        a
        trying fruit[-6]
        b
        trying fruit[-7]
        Traceback (most recent call last):
          File "p.py", line 5, in <module>
            letter = fruit[index-1]
        IndexError: string index out of range
    '''
    
    index = 0
    fruit="banana"
    while index < len(fruit):
        print('trying fruit[{}]'.format(index - 1))  ################ inserted this diagnostic
        letter = fruit[index-1]
        print(letter)
        index = index-1
    Thanks b49P23TIvg but when I execute your code it's still gives the index error.
  6. #4
  7. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,946
    Rep Power
    509
    We had hoped that you would look at the output created by the slightly modified program to realize that
    'banana'[-7]
    causes the index error. And that you might try to do this by hand, and that you might thereby ask yourself
    "how did the position indexed become -7?"
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2017
    Posts
    3
    Rep Power
    0
    Originally Posted by b49P23TIvg
    We had hoped that you would look at the output created by the slightly modified program to realize that
    'banana'[-7]
    causes the index error. And that you might try to do this by hand, and that you might thereby ask yourself
    "how did the position indexed become -7?"
    I tried but I failed

IMN logo majestic logo threadwatch logo seochat tools logo