Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IndexError: tuple index out of range ----- Python

Please Help me. I'm running a simple python program that will display the data from mySQL database in a tkinter form...

from Tkinter import * import MySQLdb  def button_click():     root.destroy()  root = Tk() root.geometry("600x500+10+10") root.title("Ariba")  myContainer = Frame(root) myContainer.pack(side=TOP, expand=YES, fill=BOTH)  db = MySQLdb.connect ("localhost","root","","chocoholics") s = "Select * from member" cursor = db.cursor() cursor.execute(s) rows = cursor.fetchall()  x = rows[1][1] + " " + rows[1][2] myLabel1 = Label(myContainer, text = x) y = rows[2][1] + " " + rows[2][2] myLabel2 = Label(myContainer, text = y) btn = Button(myContainer, text = "Quit", command=button_click, height=1, width=6)  myLabel1.pack(side=TOP, expand=NO, fill=BOTH) myLabel2.pack(side=TOP, expand=NO, fill=BOTH) btn.pack(side=TOP, expand=YES, fill=NONE) 

Thats the whole program....

The error was

x = rows[1][1] + " " + rows[1][2] IndexError: tuple index out of range  y = rows[2][1] + " " + rows[2][2] IndexError: tuple index out of range 

Can anyone help me??? im new in python.

Thank you so much....

like image 980
MSanz Avatar asked Nov 30 '13 03:11

MSanz


People also ask

How do you fix tuple index out of range in Python?

The IndexError: Python tuple index out of range error occurs when you try to access an item in a tuple that does not exist. To solve this problem, make sure that whenever you access an item from a tuple that the item for which you are looking exists.

What is tuple index Python?

Python Tuple index() Method The index() method finds the first occurrence of the specified value. The index() method raises an exception if the value is not found.

Is indexing allowed in tuple in Python?

Tuple IndexingWe can access elements in a tuple in the same way as we do in lists and strings. Hence, we can access elements simply by indexing and slicing. Furthermore, the indexing is simple as in lists, starting from the index zero.

What causes index error in Python?

What Causes IndexError. This error occurs when an attempt is made to access an item in a list at an index which is out of bounds. The range of a list in Python is [0, n-1], where n is the number of elements in the list.


1 Answers

Probably one of the indices is wrong, either the inner one or the outer one.

I suspect you meant to say [0] where you said [1], and [1] where you said [2]. Indices are 0-based in Python.

like image 144
glglgl Avatar answered Oct 19 '22 18:10

glglgl