python变量范围问题
我被python中的范围解析所困扰。 让我先解释一下代码:python变量范围问题,python,Python,我被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. Inser
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
回溯:
文件“tatamotors.py”,第5行,在
类服务数据库:
serv_db中第38行的文件“tatamotors.py”
carreg=reg
名称错误:未定义名称“reg”
我很确定我犯了一些错误。这里是n00b。放松点。谢谢:)
编辑我已加入所有相关函数和类。我还包括了相关函数。在类上调用方法时显式传递self是错误的。当原始输入返回字符串时,将ch与整数进行比较是另一个错误 试一试 反而 你在adnewcar中也有一个错误的打印 但即便如此,在修复了所有这些之后,我也无法重现你的名字错误。你真的需要编辑你的问题
- 更多代码(至少整个类)
- 错误的完整回溯
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()
你需要这三个:
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中保持缩进一致,否则解释器将很难跟踪您的作用域。是复制错误还是缩进错误?(我想)方法与类定义对齐,而不是缩进一级 也许您正在混合制表符和空格
顺便说一句,如果您正确定义了类,那么应该调用self.additem()而不是additem()(adnewcar也是如此)(self,reg,目前它可以工作,因为它实际上不是一个方法,而是一个模块级函数。请完全按照异常出现的样子复制,并进行完全回溯。尝试使用
-t
命令行选项在选项卡保姆模式下运行解释器。这可能是由打印sta的缩进问题引起的tement.无济于事。它仍然说'reg'这个名字没有定义adnewcar(self,reg)
与self.adnewcar(reg)
非常接近。文档声明x.f()
相当于x.f(x)
其中x
绑定到类的一个实例x
。在任何情况下,您可能是指self.adnewcar(reg)
相反。D.Shawley:是的,在您评论时正在修复:-)。问题是您需要编写self.method才能找到它,然后您就得到了臭名昭著的takes,正好有2个参数(给出3个)例外。就是这样。整个类现在都在讨论中。非常感谢,这对我来说现在也很有效。有一件事是在我写入数据库后,它给了我这样一个警告:第1行'reg'列的数据被截断。然后它显示了行#41。但它写入数据库很好。我是否又做错了什么?
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.