Python 学习过程问题记录
一: 学习类和对象中的问题
1. 写类的时候,我会按流程图先把各个方法,属性和参数先注释好,然后去填充代码.不过每个方法写完,下面要写个pass,要不会报错:
错误代码: IndentationError: expected an indented block
1 class auto_order: 2 order={'湘菜':['1: 攸县香干','2: 农家一碗香'],'川菜':['1: 重启小面','2: 麻辣小龙虾'],'最近菜系':[]} 3 4 #类参数 5 def __init__(self,cai_class): 6 self.cai_class = cai_class 7 8 #点菜操作 9 def order_select(self,order_con = True): #方法下是空的,返回 IndentationError: expected an indented block. 10 11 order_me = auto_order('湘菜') 12 print(order_me.order)
2. continue后面行代码不执行
我写了一个方法,比如点菜,我想把点菜的结果返回,然后怕代码有错误,然后在continue下面加了print,想打印看看是否拿到值,结果不打印.
报错代码: 无报错, continue直接结束了方法运行.
1 class auto_order: 2 order={'湘菜':['1: 攸县香干','2: 农家一碗香'],'川菜':['1: 重启小面','2: 麻辣小龙虾'],'最近菜系':[]} 3 4 #类参数 5 def __init__(self,cai_select): 6 self.cai_select = cai_select 7 8 9 #菜单调用 10 def menu_show(self): 11 self.cai_names = self.order[self.cai_class] 12 for cai_name in self.cai_names: 13 print(cai_name) 14 15 #点菜操作 16 def order_select(self,order_con = True): 17 if order_con == True: 18 cai_select = input('请选择菜系: \n') 19 20 21 elif order_con == False: 22 cai_select = input('请输入菜名: \n') 23 24 return cai_select 25 print(cai_select) 26 27 28 29 30 31 order_me = auto_order('湘菜') 32 order_me.order_select(False)
返回结果未执行25行print语句:
所以代码修改如下:
class auto_order: order={'湘菜':['1: 攸县香干','2: 农家一碗香'],'川菜':['1: 重启小面','2: 麻辣小龙虾'],'最近菜系':[]} #类参数 def __init__(self,cai_select): self.cai_select = cai_select #菜单调用 def menu_show(self): self.cai_names = self.order[self.cai_class] for cai_name in self.cai_names: print(cai_name) #点菜操作 def order_select(self,order_con = True): if order_con == True: cai_select = input('请选择菜系: \n') elif order_con == False: cai_select = input('请输入菜名: \n') print(cai_select) return cai_select order_me = auto_order('湘菜') order_me.order_select(False)
返回结果,执行了print语句: