Hi everybody,

Can you please help me to fix this error:

For exit press 'q'
Please enter name :Jack London
For exit press 'q'
Please enter name :C.S. Lewis
For exit press 'q'
Please enter name :q
Traceback (most recent call last):
  File "/home/serg/python/First/start/Q1.py", line 28, in <module>
    print row[0], row[1]
TypeError: 'NoneType' object is not subscriptable

And this is the code:

import MySQLdb as mdb
import sys

con = mdb.connect('localhost',
with con:
    cur = con.cursor()
    cur.execute('DROP TABLE IF EXISTS Writers')
                CREATE TABLE Writers
                    Id INT PRIMARY KEY AUTO_INCREMENT,
                    Names VARCHAR(25)
    while True:
        print "For exit press 'q'"
        name = raw_input("Please enter name :")
        if name == 'q': break
        cur.execute('INSERT INTO Writers(Names) VALUES("%s")' % name)

    numrows = int(cur.rowcount)

    for i in range(numrows):
        row = cur.fetchone()
        print row[0], row[1]

You get the error, because in line 21 you exit the loop, and not the whole with clause.
Either you make a sys.exit at this line, or make a check before line 26: if numrows>0.

Is your usage of with related to this rule in PEP8:

Context managers should be invoked through separate functions or methods whenever they do something other than acquire and release resources. For example:


with conn.begin_transaction():


with conn: