Python Forum
IndexError: list index out of range
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IndexError: list index out of range
#1
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)
Error:
Traceback (most recent call last): File "/home/ali/Downloads/Attribute-Based-Encryption-master/LSSS_t,n.py", line 275, in <module> runPackage([], []) File "/home/ali/Downloads/Attribute-Based-Encryption-master/LSSS_t,n.py", line 259, in runPackage predefinedVars, returnK = initiateScheme(predefinedVars) File "/home/ali/Downloads/Attribute-Based-Encryption-master/LSSS_t,n.py", line 56, in initiateScheme recovered_k = runScheme(t, n, secret_k, q, Field) File "/home/ali/Downloads/Attribute-Based-Encryption-master/LSSS_t,n.py", line 112, in runScheme 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]) IndexError: list index out of range
Reply
#2
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?
Reply
#3
@ bowlofred Yeah I got the error is to correct for j in range(0, t):
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  IndexError: list index out of range dolac 4 405 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 604 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  TypeError: list indices must be integers or slices, not range Anldra12 2 717 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  matplotlib x axis range goes over the set range Pedroski55 5 1,367 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  IndexError: list index out of range rf_kartal 6 1,441 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  Python Error List Index Out of Range abhi1vaishnav 3 1,117 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  IndexError: list index out of range Laplace12 1 1,457 Jun-22-2021, 10:47 AM
Last Post: Yoriz
  IndexError: list index out of range brunolelli 11 3,604 Mar-25-2021, 11:36 PM
Last Post: brunolelli
  index of range, but data prints out mrc06405j 1 1,374 Mar-25-2021, 07:20 PM
Last Post: buran
  Changing Index of 2 List in python giddyhead 0 1,061 Mar-05-2021, 05:45 PM
Last Post: giddyhead

Forum Jump:

User Panel Messages

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