Edison Edison - 2 months ago 12
Python Question

Indentation error in python and index out of range error

I am getting an indentation error. Can anyone help me to fix it and also getting

tuple index out of range error
too

here is my code

def POST(self):
form = web.input(name="a", newname="s", number="d")
conn = MySQLdb.connect(host= "localhost", user="root", passwd="", db="testdb")
x = conn.cursor()
x.execute("SELECT * FROM details WHERE name = '%s'" % (form.name))
conn.commit()
items = x.fetchall()
for row in items:
print row[0], row[1],row[2]
print("<table border='1'>")
print("<tr>")
print("<th>name</th>")
print("<th>address</th>")
print("<th>number</th>")
print("</tr>")
print("<tr>")
print("<td>{0}</td>".format(row[0]))
print("<td>{1}</td>".format(row[1]))
print("<td>{2}</td>".format(row[2]))
print("</tr>")
print("</table>")

conn.rollback()
conn.close()
#return render.index(items)

if __name__ == "__main__":
app.run()


am getting error in these lines

print("</tr>")
print("</table>")

Answer

This is a very basic error.If you are using sublime text.Please select all the rows. In the beginning of each line of def POST, there will be two patterns. One is '_____' and '.......' .The pattern should be similar for whole 'def POST(self):' Please make sure this and error will get removed.

Copy and paste this code:

    def POST(self):
            form = web.input(name="a", newname="s", number="d")
            conn = MySQLdb.connect(host= "localhost", user="root", passwd="", db="testdb")
            x = conn.cursor()
            x.execute("SELECT * FROM details  WHERE name = '%s'" % (form.name))
            conn.commit()
            items = x.fetchall()
            for row in items:
                print row[0], row[1],row[2]
                print("<table border='1'>")
                print("<tr>")
                print("<th>name</th>")
                print("<th>address</th>")
                print("<th>number</th>")
                print("<tr>")
                print("<td>{0}</td>".format(row[0]))
                print("<td>{1}</td>".format(row[1]))
                print("<td>{2}</td>".format(row[2]))
                print("</tr>")
                print("</table>")

                conn.rollback()
                conn.close()
                #return render.index(items)

    if __name__ == "__main__":
        app.run()

Thanks.