上课内容对程序进行了讲解后自行敲出的程序,心得一二,记录下来。

程序要求:

1. 打印省、市、县三级菜单

2.可返回上一级

3.随时退出程序

自行敲出的程序如下:

menu = {
'北京':{
'海淀':{
'五道口':{
'soho':{},
'网易':{},
'google':{}
},
'中关村':{
'爱奇艺':{},
'汽车之家':{},
'youku':{},
},
'上地':{
'百度':{},
},
},
'昌平':{
'沙河':{
'老男孩':{},
'北航':{},
},
'天通苑':{},
'回龙观':{},
},
'朝阳':{},
'东城':{},
},
'上海':{
'闵行':{
"人民广场":{
'炸鸡店':{}
}
},
'闸北':{
'火车战':{
'携程':{}
}
},
'浦东':{},
},
'山东':{},
}
exit_flag= False
while not exit_flag :
for i in menu:
print(i)
choice=input("please come in level1...")
if choice in menu:
while not exit_flag :
for i2 in menu[choice]:
print(i2)
choice2=input("please come in level2...")
if choice2 in menu[choice]:
while not exit_flag :
for i3 in menu[choice][choice2]:
print(i3)
choice3=input("please come in level3...")
if choice3 in menu[choice][choice2]:
for i4 in menu[choice][choice2][choice3]:
print(i4)
choice4=input("ending...if you want return,please press'b',if you want quit ,please press 'q'... ")
if choice4 =='b':
pass
elif choice4 =='q':
exit_flag = True
if choice3 == "b":
break
elif choice3 == "q":
exit_flag = True
if choice2 == "b":
break
elif choice2 == "q":
exit_flag = True

在听取讲解时,并不是很明白为什么要设置FLAG,而不是直接写NOT TRUE。在自己敲代码时领会到,如果不使用flag,在需要退出时就需要不断地设置not true的新标量。另外,在敲代码时查询了下pass在python中的含义——1.空语句,表示donothing 2.保证格式和语义完整。在敲代码时发现自己还有个致命的问题,循环太多时,代码还是不能对齐,运行时不断报错。
不过这个程序还有很多不完善的地方,比如说:1.如何在进入二级菜单时就选择返回或者退出?2.每个城市的菜单不平衡,如何根据当级菜单判断是否有下一级菜单?...暂时我的水平还无法解决,等待之后学习的内容更丰富以后继续完善这个代码,如果有建议和思路欢迎留言!

 

posted on 2017-10-22 10:21  小菜鸟成长之路  阅读(349)  评论(0编辑  收藏  举报