i am stuck at scope resolution in python. let me explain a code first:
class serv_db:
def __init__(self, db):
self.db = db
self.dbc = self.db.cursor()
def menudisp (self):
print"Welcome to Tata Motors"
print"Please select one of the options to continue:"
print"1. Insert Car Info"
print"2. Display Car Info"
print"3. Update Car Info"
print"4. Exit"
menu_choice = raw_input("Enter what you want to do: ")
if menu_choice==1: additem()
elif menu_choice==2: getitem()
elif menu_choice==3: edititem()
elif menu_choice==4: sys.exit()
def additem (self):
reg = raw_input("\n\nTo continue, please enter the Registration # of car: ")
print"There are 3 books in our database:"
print"1. Job Card"
print"2. Car"
print"3. Customer"
ch = raw_input("\nEnter your choice: ")
if ch==1: adnewjob()
elif ch==2: adnewcar(self, reg)
elif ch==3: adnewcust()
def adnewcar ( self, reg ):
print "adding info to database: car"
carreg = reg #error here
mftr = raw_input("Enter the Manufacturer of your car: ")
model = raw_input("Enter the Model of your car: ")
car_tb = (carreg,mftr,model)
#writing to DB
self.dbc.execute("insert into car(reg, mftr, model) values(%s,%s,%s)", car_tb)
def main():
db = MySQLdb.connect(user="root", passwd="", db="tatamotors")
service = serv_db(db)
service.menudisp()
if __name__ == '__main__':
main()
i am inputting a registration num into the variable reg now based upon the user's choice one of three functions is performed. i havent yet created the adnewjob() and adnewcust() functions yet. the adnewcar() is ready. when i try to pass the value down to the adnewcar() function, it gives an error saying:
This is the entire traceback:
Traceback <most recent call last>:
File "tatamotors.py", line 5, in <module>
class serv_db:
File "tatamotors.py", line 38, in serv_db
carreg = reg
Name Error: name 'reg' is not defined
i am pretty sure i am making some mistake. n00b here. go easy. thanks :)
EDIT i have joined all the relevant functions and classes. i have also included the related functions too.