IndexError: list index out of range - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: IndexError: list index out of range (/thread-37116.html) |
IndexError: list index out of range - Anldra12 - May-03-2022 A common error but i don't know the reason why list index out of range def runScheme(t_str, n_str, k, q, Field): x_subi = [0] a_subj = [0] pShares = [0] pShares_regex = [0] t = int(t_str) n = int(n_str) for i in range(1, n + 1): print("ran") x = getDistinctX(x_subi, Field) x_subi.append(x) print("x All the values :", x_subi) for j in range(1, t): ind = random.randint(0, q) a_subj.append(Field[ind]) print(a_subj) #Add or subtract a_i as needed print("a_0 value:", a_subj[0], "a_1 value", a_subj[1],"a_2 vlaue:", a_subj[2], "a_3 value:", a_subj[3]) # print("a_1 value:", a_subj[0], "a_2 value", a_subj[1], "a_3 vlaue:", a_subj[2]) for i in range(1, n + 1): x = x_subi[i] print("x_i value :",x) polynomialSum = k print(k) for j in range(1, t): a = a_subj[j] print("a:", a) exponent = math.pow(2, j) print("exponent:", exponent) polynomialSum += a * exponent print("polynomialSum:", polynomialSum) regEx = polynomialSum % q # print("(%d, %d)" % (x, 19)) pShares_regex.append(regEx) print("all f(x) :", pShares_regex) pShares.append(polynomialSum)
RE: IndexError: list index out of range - bowlofred - May-03-2022 a_subj starts as a 1-element list. It gets t-1 extra elements as it goes through the for j in range(1, t) loop.So if t is 3 or less, there will be 3 or less elements in a_subj. Then when you get to the print() statement, the request to print a_subj[3] will fail, since that index doesn't exist. You are printing out the value of a_subj on line 21. What is the value? RE: IndexError: list index out of range - Anldra12 - May-03-2022 @ bowlofred Yeah I got the error is to correct for j in range(0, t): |