Bottom Page

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
Yoriz wrote Jul-04-2019, 06:35 PM:
Please post all code, output and errors (in it's entirety) between their respective tags. I did it for you this time, Here are instructions on how to do it yourself next time.
Quote
#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
Quote
#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
Quote
#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
Quote
#5
It explains in that link i provided. Instead of looping through indexes you loop directly the sequence.
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  String index out of range - help please DudleyDiccle 7 158 Mar-27-2020, 12:10 AM
Last Post: DudleyDiccle
  Get the codemap combinations but got tuple index out of range haganmao 1 155 Feb-21-2020, 02:51 AM
Last Post: michael1789
  Define a range, return all numbers of range that are NOT in csv data KiNeMs 18 610 Jan-24-2020, 06:19 AM
Last Post: KiNeMs
  IndexError: index 8 is out of bounds for axis 0 with size 8 Help_me_Please 6 1,699 Jan-03-2020, 01:51 PM
Last Post: Help_me_Please
  How to get the index of a character from a string chihaya 1 180 Dec-03-2019, 12:54 PM
Last Post: buran
  Unicode string index problem luoheng 6 263 Nov-23-2019, 03:04 PM
Last Post: luoheng
  IndexError: List index out of range issue Adem 1 802 Nov-01-2019, 10:47 PM
Last Post: ichabod801
  IndexError: list index out of range bug? CaptainCsaba 4 1,514 Oct-18-2019, 12:38 PM
Last Post: CaptainCsaba
  MAWS.py PRGM:IndexError: list index out of range bcramer 0 259 Aug-28-2019, 04:45 PM
Last Post: bcramer
  index out of range rwahdan 3 723 Jul-04-2019, 08:11 PM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)