首页 > 代码库 > Day3.三级菜单+购物车打印格式优化

Day3.三级菜单+购物车打印格式优化

程序: 三级菜单
要求: 1.打印省、市、县三级菜单
2.可返回上一级
3.可随时退出程序

menu = {
    北京:{海淀:{五道口:{soho:{},
                                网易:{},
                                google:{}},
                     中关村:{爱奇艺:{},
                                汽车之家:{},
                                youku:{},},
                     上地:{百度:{},},},
            昌平:{沙河:{老男孩:{},
                             北航:{},},
                    天通苑:{},
                    回龙观:{},},
            朝阳:{},
            东城:{},
            },
    上海:{闵行:{人民广场:{炸鸡店:{}}},
            闸北:{火车站:{携程:{}}},
            浦东:{},
            },
    山东:{},
}
flag_exit=True
while flag_exit:
    for i in menu:
        print(i)
    choice1 = input("请输入>").strip()
    if choice1 in menu:
        while flag_exit:
            next_level1=menu[choice1]
            for i in next_level1:
                print(i)
            choice2 = input("请输入>>").strip()
            if choice2 in next_level1:
                while flag_exit:
                    next_level2= next_level1[choice2]
                    for i in next_level2:
                        print(i)
                    choice3 = input("请输入>>>").strip()
                    if choice3 in next_level2:
                        while flag_exit:
                            next_level3=next_level2[choice3]
                            for i in next_level3:
                                print(i)
                            choice4 = input("请输入>>>>").strip()
                            if choice4 == "q":
                                flag_exit=False
                                continue
                            elif choice4=="b":
                                break
                            else:
                                continue
                    elif choice3 =="q":
                        flag_exit=False
                        continue
                    elif choice3 =="b":
                        break
                    else:
                        continue
            elif choice2 =="q":
                flag_exit=False
                continue
            elif choice2 =="b":
                break
            else:
                continue
    elif choice1=="q":
        flag_exit=False
        continue
    else:
        continue

技术分享

 1 product_list=[["苹果",10],["椰子",200],["菠萝",20],["火龙果",50],]
 2 shopping_cart={}
 3 salary=int(input("请输入你的薪资:"))
 4 while True:
 5     index=0
 6     for product in product_list:
 7         print(index,product)
 8         index+=1
 9     choice=input(">>>:")
10     if choice.isdigit():
11         choice=int(choice)
12         if choice>= 0 and choice<(len(product_list)):
13             product=product_list[choice]
14             if salary >= product[1]:
15                 if product[0] not in shopping_cart:
16                     shopping_cart[product[0]] = [product[1],1]
17                 else:
18                     shopping_cart[product[0]][1] +=1
19                 salary -= product[1]
20                 print(product[0]+"已成功购买,您还剩余"+str(salary)+"元钱")
21             else:
22                 print("余额不足,还差"+str(product[1]-salary)+"元钱才能购买"+product[0])
23         else:
24             print("商品不存在!")
25     elif choice == "q":
26         print("-------商品列表-------")
27         print("id   商品  数量  单价  总价")
28         id_number=1
29         total=0
30         for i in shopping_cart:
31             print("%s\t%s\t%s\t%s\t%s\t"
32                   %(id_number,
33                     i,
34                     shopping_cart[i][1],
35                     shopping_cart[i][0],
36                     shopping_cart[i][0]*shopping_cart[i][1]))
37             id_number+=1
38             total +=shopping_cart[i][0]*shopping_cart[i][1]
39         print("您的余额为:",salary)
40         print("您总花费为:",total)
41         print("---------end----------")
42         break
43     else:
44         print("不能识别该选项")

 

 

Day3.三级菜单+购物车打印格式优化