我在python中遇到了范围分辨率。 让我先解释一下代码:
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()
我现在根据用户的选择在变量reg
中输入注册号,执行三个功能之一。我还没有创建adnewjob()
和adnewcust()
函数。 adnewcar()
准备好了。当我尝试将值传递给adnewcar()函数时,它会出错:
这是整个追溯:
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
我很确定我犯了一些错误。在这里。放轻松。谢谢:))
编辑我加入了所有相关的功能和课程。我也包括了相关的功能。
答案 0 :(得分:2)
在类上调用方法时显式传递self是错误的。当raw_input返回一个字符串
时,将ch与整数进行比较是另一个错误尝试
elif ch=='2': self.adnewcar(reg)
代替
在adnewcar中你也有一个印记错误。
但即便如此,在修复所有这些之后我无法重现你的NameError。你真的需要用
编辑你的问题你有机会使用Python 3.0吗?你的环境是什么?
对于记录,这适用于我,使用Python 2.5.2
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': self.additem()
elif menu_choice=='2': self.getitem()
elif menu_choice=='3': self.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': self.adnewjob()
elif ch=='2': self.adnewcar(reg)
elif ch=='3': self.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()
答案 1 :(得分:0)
你需要这三个:
if menu_choice==1: self.additem() # 1: self.
elif ch=='2': self.adnewcar(reg) # 2: self. instead of (self, reg)
print "adding info to database: car" # 3: indented.
始终记住在整个.py中保持缩进一致,否则翻译人员很难跟踪您的范围。
答案 2 :(得分:0)
是复制错误还是你的缩进错了? (我想)方法与类定义对齐,而不是缩进一个级别。
也许你正在混合标签和空格?
顺便说一句,如果你正确定义你的类,你应该调用 self.additem()而不是 additem()(同样适用于 adnewcar(自我) ,reg ,在它工作的那一刻,因为它实际上不是一个方法,而是一个模块级函数。