第五天
今天讲的内容基本上就是讲购物车程序的两种写法了,然后又重复了一次转码和编码(我现在感觉没什么必要老师也没有讲清楚)
繁琐的方法
# __author:凌风之沐
# DATE: 2020/1/31
exit_flag = Falsmenu = {
'北京':{
'朝阳':{
'国贸':{
'CCIC':{},
'HP':{},
'渣打银行':{},
'CCTV':{}
},
'望京':{
'陌陌':{},
'奔驰':{},
'360':{},
},
'三里屯':{
'优衣库':{}
}
},
'昌平':{
'沙河':{
'老男孩':{},
'阿泰包子':{},
},
'天通苑':{
'链家':{},
'我爱我家':{}
},
'回龙观':{}
},
'海锭':{
'五道口':{
'谷歌':{},
'网易':{},
'搜狐':{},
'搜狗':{},
'快手':{}
},
'中关村':{
'优酷':{},
'爱奇艺':{},
'汽车之家':{},
'新东方':{},
'QQ':{}
}
}
},
'上海':{
'浦东':{
'陆家嘴':{
'CICC':{},
'高盛':{},
'摩根':{}
},
'外滩':{}
},
'闵行':{},
'静安':{}
},
'山东':{
'济南':{},
'德州':{
'乐岭':{},
'平原':{}
},
'青岛':{}
}
}e
back_flag = False
while not back_flag and not exit_flag :
for key in menu:
print(key)
choice1 = input('>>1:').strip()
if choice1 == 'x':
exit_flag = True
if choice1 == 'q':
back_flag = True
if choice1 in menu:
while not back_flag and not exit_flag:
for key2 in menu[choice1]:
print(key2)
choice2 = input('>>2:').strip()
if choice2 == 'x':
exit_flag = True
if choice2 == 'q':
back_flag = True
if choice2 in menu[choice1]:
while not back_flag and not exit_flag:
for key3 in menu[choice1][choice2]:
print(key3)
choice3 = input('>>3:').strip()
if choice3 == 'x':
exit_flag = True
if choice3 == 'q':
back_flag = True
if choice3 in menu[choice1][choice2]:
while not back_flag and not exit_flag:
for key4 in menu[choice1][choice2][choice3]:
print(key4)
choice4 = input('>>4:').strip()
print('last level')
if choice4 == 'x':
exit_flag = True
if choice4 == 'q':
back_flag = True
else:
back_flag = False
else:
back_flag = False
else:
back_flag = False
简洁的方法
current_layer = menu
parent_layers = []
while True:
for key in current_layer:
print(key)
choice = input(">>:").strip()
if len(choice)==0:continue #当我输入回车的时候继续让我们输入,不会输出无此项
if choice in current_layer:
parent_layers.append(current_layer)
current_layer = current_layer[choice]
elif choice == 'b':
if parent_layers:
current_layer=parent_layers.pop()
else:
print('无此项')
感觉现在很不耐烦。
讲一下自己在从写菜单开始遇到的困难。
1、菜单我写了两遍才写对,第一次是因为分层不明确,加上中文输入和英文输入符号出错:{}“”中文英文的输入都是不相同的
2、第一个程序确实很繁琐,我都是一步步的把功能加上去的,感觉很笨
3、
for key in menu:
print(key)
choice1 = input('>>1:').strip()
在写着一个循环的时候我的for和choice没有对齐导致只打印北京出来,没有把三个都写出来,在代码完全一样的时候一定要注意是否对称了,才能实现自己想要实现的功能
4、标志位,and的使用
5、.strip()的功能是把很长的循环只写一次
6、menu【】【】【】连续进入下一个菜单
7、while
else的运用
8、
.append()的使用,他加入在列表的最后面
9、.pop()删除列表的最后一个
10、列表,字典有点搞混淆

浙公网安备 33010602011771号