python 循环嵌套

需求如下:

  1.

 #定义商品列表类
# def shangpin_list():
# for i in com_list:
# print(com_list.index(i),end=" ")
# for j in i:
# print(j,end=" ")
# print()
# else:
# return True #程序入口 salary = int(input("请输入您的工资:"))
com_list = [['iphone6s',5000],['mac book',9000],['coffee',32],['python book',80]]
shopping_cart_list = [] #购物车
cum_money = [] #账单
car_list = []
com_count = 0
while True:
if salary >= 0:
for i in com_list: #展示商品
print(com_list.index(i),end=" ")
for j in i:
print(j,end=" ")
print()
gm = input('''想买点啥?
输入商品编号即可购买,输入quit退出商城。
请输入》》》''') #开始购物
if gm != 'quit':
try:
gm = int(gm)
salary = salary - int(com_list[gm][1])
if salary >= 0:
com = com_list[gm][0] #购买的商品
com_money = com_list[gm][1]
shopping_cart_list.append([com,com_money]) #购买的商品
com_count = com_count+com_money
print('购买',com,'您需要支付',com_money,'元--->扣除后余额为:%d'%salary)
else:
print("余额不足,骚年快去挣钱吧!")
except ValueError or IndexError as err:
print("输入有误呕!")
else:
print( '本次您购买了以下商品,共计花费:%d元'%com_count)
for ij in shopping_cart_list:
print( '%s %s元'%((ij[0]),ij[1]))
else:
exit("您还有余额%d欢迎下次光临"%salary)
continue

购物车

 #!/user/bin/env python3
# -*- coding=utf-8 -*-
#__author__:gq
#date:2016/8/24 0024 dic = {'北京':{'昌平':['沙河','西二旗','西三旗'],'海淀':['圆明园']}, '河北':{'石家庄':['新华区'],'衡水':['老白干']},'广州':{'深圳':['华强北']}}
# for index in dic.items():
# print(index)
#一级菜单
def menu_1():
dic_01={}
while True:
print('一级菜单'.center(55,'#'))
for index,menu_name in enumerate(dic):
print(index,menu_name)
dic_01.setdefault(index,menu_name)
print(dic_01)
chose = input('输入菜单序号进入下级菜单,输入b反回上一级菜单')
if chose.isdigit():
chose = int(chose)
if dic_01.get(chose) == None:
print('没有这%s这个菜单项,请重新输入!'%chose)
continue
else:
#print(dic_01.get(chose))
menu_2(dic_01.get(chose))
elif chose == 'q':
print("程序退出")
else:
print("您的输入有误,请重新输入!") #二级菜单
def menu_2(name):
dic_2 = {}
while True:
flag = False
print("%s 二级菜单".center(55,'#')%name)
for index,menu in enumerate(dic[name]):
print(index,menu)
dic_2.setdefault(index,menu)
print(dic_2)
chose2 = input('输入菜单序号进入下级菜单,输入b反回上一级菜单,输入q退出')
if chose2.isdigit(): #判断用户输入的内容是否是数字样式
chose2 = int(chose2)
#print(dic_2.get(chose2))
if dic_2.get(chose2) == None:
print('没有这%s这个菜单项,请重新输入!'%chose2)
else:
print(dic_2.get(chose2))
flag = menu_3(name,dic_2.get(chose2))
if flag:
return True
else:
continue
elif chose2 == 'q': #用户输入‘q'则退出程序
print("程序退出")
elif chose2 == 'b': #用户输入'b'则反回上级菜单
break
else:
print("您的输入有误,请重新输入!") #三级菜单
def menu_3(name1,name2):
dic_3 ={}
while True:
print('%s 三级菜单'.center(55,'#')%name2)
for index3,menu3 in enumerate(dic[name1][name2]):
print(menu3)
dic_3.setdefault(index3,menu3)
print(dic_3)
chose3 = input('输入b反回上一级菜单,输入m反回主菜单,输入q退出')
if chose3 == 'q':
print("程序退出")
exit()
elif chose3 == 'm':
return True
elif chose3 == 'b':
return False
else:
print("您的输入有误,请重新输入!") while True:
menu_1()

三级菜单

 dic = {'北京':{'昌平':{'沙河':{},'回龙观':{},'巩华城':{}},
'海淀':{'圆明园'}},
'河北':{'石家庄':{'新华区'},
'衡水':{'老白干'}},
'广州':{'深圳':{'华强北'}}}
dic1 = dic
parent_list=[]
while True:
for i in dic1:
print(i)
choose = input('>>>').strip()
if len(choose)==0:continue
if choose in dic1:
parent_list.append(dic1)
dic1 = dic1[choose]
elif choose == 'b':
if parent_list:
dic1 = parent_list.pop()
else:
print("无此项")

三级菜单进化版

上一篇:Java魔法堂:打包知识点之jar


下一篇:[译]Java Thread join示例与详解