Python Forum
IndexError: string index out of range ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IndexError: string index out of range ?
#1
Hello all,

I just start to learn python, working on the chapter of THE BAGELS DEDUCTION GAME.

No matter how hard I am, the order just failed.

Always showing:
Error:
Traceback (most recent call last): File "/Users/qiyin/Downloads/Python37/bagels.py", line 56, in <module> print(getClues(guess, secretNum)) File "/Users/qiyin/Downloads/Python37/bagels.py", line 20, in getClues if len(guess[i]) == len(secretNum[i]): IndexError: string index out of range
This is the order of Line 20 and line 56
Line 20:
    clues = []
    for i in range(len(guess)):
        if guess[i] == secretNum[i]:
            clues.append('Fermi')
        elif guess[i] in secretNum:
            clues.append('Pico')
Line56
while True:
    secretNum = getSecretNum()
    print('I have thought up a number. You have %s guesses to get it.' % (MAX_GUESS))

    guessesTaken = 1
    while guessesTaken <= MAX_GUESS:
        guess = ''
        while len(guess) != NUM_DIGITS or not isOnlyDigits(guess):
            print('Guess #%s: ' % (guessesTaken))
            guess = input()

        print(getClues(guess, secretNum))
        guessesTaken += 1
Anyone could tell me what was happed?

Thanks a lot..

Best
Reply
#2
That error happens when a string index used is not a valid index of the string.

In the next example the last index of 'string' is 5, using 6 gives the error.

>>> 'string'[6]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range
Reply
#3
Based on your error secretNum[i] is going out of index. Although you shoukdnt be using range(len(sequence)) ever in python.

https://python-forum.io/Thread-Basic-Nev...n-sequence
Recommended Tutorials:
Reply
#4
(Jul-04-2019, 06:42 PM)Yoriz Wrote: That error happens when a string index used is not a valid index of the string.

In the next example the last index of 'string' is 5, using 6 gives the error.

>>> 'string'[6]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range

Hello Yoriz,

Thank you for your help about the post and reply. How should I modify this order to make it done?

Best

(Jul-04-2019, 06:44 PM)metulburr Wrote: Based on your error secretNum[i] is going out of index. Although you shoukdnt be using range(len(sequence)) ever in python.

https://python-forum.io/Thread-Basic-Nev...n-sequence


Thank you very much for your help. I just wrote the order from the book, and not totally understood what was this order meaning? Based on the orders I provided, how to modify it to make it done?

Best
Reply
#5
It explains in that link i provided. Instead of looping through indexes you loop directly the sequence.
Recommended Tutorials:
Reply
#6
Hi,

Quote:I just wrote the order from the book,
If this code is really shown in a book -> throw away the book, it teaches bad Python. Iterating over an iterable with range(len(...)) as an antipattern. Also the style your variable names are written is not the pythonic way to do it.

Regards, noisefloor
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  IndexError: index 10 is out of bounds for axis 0 with size 10 Mehboob 11 1,953 Sep-14-2023, 06:54 AM
Last Post: Mehboob
Thumbs Down I hate "List index out of range" Melen 20 3,161 May-14-2023, 06:43 AM
Last Post: deanhystad
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 5,969 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  IndexError: list index out of range dolac 4 1,845 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,280 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,410 May-03-2022, 01:39 PM
Last Post: Anldra12
  matplotlib x axis range goes over the set range Pedroski55 5 3,111 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  IndexError: list index out of range rf_kartal 6 2,762 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  Python Error List Index Out of Range abhi1vaishnav 3 2,239 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  IndexError: list index out of range Laplace12 1 2,186 Jun-22-2021, 10:47 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020