Search code examples
python-3.xrangeindex-error

some issues with index range, python student project?


def f3():

    root.withdraw()
    viewest.deiconify()
    #import cx_Oracle
    con=None
    cursor=None
    try:
        con=cx_Oracle.connect("system/abc123")
        cursor=con.cursor()
        sql="select * from students"
        cursor.execute(sql) 
        msg=""
        for d in data:
            msg=msg+" R: "+ str(d[0]) + " N: " + str(d[1])+" M: " + str(d[2])+ "\n"
            stData.insert(INSERT,msg)
    except cx_Oracle.DatabaseError as e:
        print("some issues",e)

    finally:
        if cursor is not None:
            cursor.close()
        if con is not None:
            con.close()
            print("Disconnected")

'''IndexError: string index out of range , msg=msg+" R: "+ str(d[0]) + " N: " + str(d[1])+" M: " + str(d[2])+ "\n" '''


Solution

  • It is hard to tell what you're trying to do here but what's the data type of data? It looks like you're not getting past the try statement (good use of a try/except to handle Exceptions!) so the issue I think lies in the way you're indexing the items (d) in data. Think about it: if data didn't exist (meaning you didn't initialize it in your code for your program to use), how can you try to index it later on? Also, it's weird that you're not getting a NameError as I don't see data defined anywhere unless you cropped out where it was defined earlier (unless my eyes are tricking me).

    General Programming principles:

    • Iterable Objects: String, List, Dictionary, etc...
    • Non-iterable Objects: Integer, Float, Boolean, etc... (depending on programming language)

    Iterable data types are ones that you can index using bracket notation ([]) so in any case, you'll need to know the types of your variables and then use the correct "processes" to work with them. Since you are indexing items in data, the data type of data therefore, needs to be an iterable too.

    In general, an IndexError means that you've tried to access/index an item in a list for example but that item didn't exist. Therefore, I think that trying to string AND index an item in an object that doesn't exist is giving you that error.

    Here's what I think would help:

    If I could see where you have data defined, I would be able to tell you where you may be going wrong, but for now, I can only recommend you going back to see whether you've indexed an item in d in data that doesn't exist.

    An example (I will assume data is a list for clarity):

    >>> data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    
    >>> data[0] # index a list in d in data
    [1, 2, 3]
    >>> data[0][1]
    2
    
    # If you try to access an index in data that doesn't exist:
    >>> data[3]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    IndexError: list index out of range
    

    I probably over-explained by now, so I'll just end this here. The above implementation was to give you an idea of how to access a list in a list correctly and when it isn't properly done what can happen. Hope this is clear!