zoukankan      html  css  js  c++  java
  • Python购物车

    product_list = [
        ['Iphone',5888],
        ['Mac Air',8000],
        ['XiaoMi',19.9],
        ['coffee',30],
        ['Tesla',820000],
        ['Bike',700],
        ['Cloth',200],   ]
    shop_car={}
    salary=int(input("请输入您的工资: "))
    while True:
        index=0
        for i in  product_list:
            print(index,i)
            index+=1
        choice=input("请输入商品编号 w保存,q 退出: ")
        if choice.isdigit():
            choice=int(choice)
            if  choice > 0 and choice <= len(product_list):
                product=product_list[choice]
                if salary >= product[1]:
                    if product[0] in shop_car: #已经购买过的商品, 键值对,键:商品 , 值:【数量 价格】
                        shop_car[product[0]][1] += 1
                    else:
                        shop_car[product[0]] = [product[1] ,1] #购物车里没有的商品 添加一条键值对,键:商品 值:【数量:值:价格】
                        salary -= product[1]                                   #{'XiaoMi': [19.9, 2], 'coffee': [30, 1]}
                else:
                    print("余额不足")
            else:
                print("您输入的商品编号不存在")
        elif choice == 'w':
            print("--------------已经购买的商品列表----------------")       #{'XiaoMi': [19.9, 2], 'coffee': [30, 1]}
            for k in shop_car:
                print( "商品:%5s 单价:%5s 数量:%5s  总价:%5s " %
                (k,
                shop_car[k][0],
                shop_car[k][1],
                shop_car[k][0]* shop_car[k][1]) )
            print("您的余额 %s" % salary)
            print("end-----------")
            break
        else:
            exit("退出程序")

     三级菜单

     1 menu = {
     2     '北京':{
     3         '海淀':{
     4             '五道口':{
     5                 'soho':{},
     6                 '网易':{},
     7                 'google':{}
     8             },
     9             '中关村':{
    10                 '爱奇艺':{},
    11                 '汽车之家':{},
    12                 'youku':{},
    13             },
    14             '上地':{
    15                 '百度':{},
    16             },
    17         },
    18         '昌平':{
    19             '沙河':{
    20                 '钓鱼岛':{},
    21                 '北航':{},
    22             },
    23             '天通苑':{},
    24             '回龙观':{},
    25         },
    26         '朝阳':{},
    27         '东城':{},
    28     },
    29     '上海':{
    30         '闵行':{
    31             "人民广场":{
    32                 '炸鸡店':{}
    33             }
    34         },
    35         '闸北':{
    36             '火车战':{
    37                 '携程':{}
    38             }
    39         },
    40         '浦东':{},
    41     },
    42     '河北':{},
    43 }
    44 
    45 while True:#一直打印一级菜单
    46     for key in menu:  #打印一级菜单的 key
    47         print(key)
    48     choice=input(">")
    49     if len(choice)==0:continue
    50     if choice == 'b': break
    51     if choice=='q':exit('退出程序')
    52     while True: #一直循环打印二级菜单
    53         lary=menu[choice]   #进入二级菜单
    54         for key in lary:
    55             print(key)
    56         choice1=input(">>")
    57         if len(choice)==0:continue
    58         if choice1 == 'b': break
    59         if choice1 == 'q': exit('退出程序')
    60         if choice1 in lary:
    61             while True:
    62                 lary2=lary[choice1]  #进入三级菜单
    63                 for key in  lary2:
    64                     print(key)
    65                 choice2=input(">>>")
    66                 if len(choice2)==0: continue
    67                 if choice2 == 'b': break
    68                 if choice2 == 'q': exit('退出程序')
    69                 if choice2 in lary2:
    70                     while True:
    71                         lary3= lary2[choice2]
    72                         for key in lary3:
    73                             print(key)
    74                         endchoice=input('b 返回上一层,q退出: ')
    75                         if endchoice == 'b': break
    76                         if endchoice == 'q': exit('退出程序')

     三级菜单改进版

     1 menu = {
     2     '北京':{
     3         '海淀':{
     4             '五道口':{
     5                 'soho':{},
     6                 '网易':{},
     7                 'google':{}
     8             },
     9             '中关村':{
    10                 '爱奇艺':{},
    11                 '汽车之家':{},
    12                 'youku':{},
    13             },
    14             '上地':{
    15                 '百度':{},
    16             },
    17         },
    18         '昌平':{
    19             '沙河':{
    20                 '钓鱼岛':{},
    21                 '北航':{},
    22             },
    23             '天通苑':{},
    24             '回龙观':{},
    25         },
    26         '朝阳':{},
    27         '东城':{},
    28     },
    29     '上海':{
    30         '闵行':{
    31             "人民广场":{
    32                 '炸鸡店':{}
    33             }
    34         },
    35         '闸北':{
    36             '火车战':{
    37                 '携程':{}
    38             }
    39         },
    40         '浦东':{},
    41     },
    42     '山东':{},
    43 }
    44 last_layers=[menu]
    45 current_layer=menu
    46 while True:
    47     for key in current_layer:
    48         print(key)
    49     choice=input(">")
    50     if len(choice)==0:continue
    51     if choice in current_layer:                    #列表套字典
    52         current_layer=current_layer[choice]
    53         last_layers=last_layers.append(current_layer) #列表添加进 本层 choice键的 内容
    54     if choice == 'b':
    55         current_layer=last_layers[-1]
    56         last_layers.pop()# l=[]
    57         if current_layer == 'q':
    58             break
    59 
    60 
    61 # me=menu['北京']
    62 # print(me)
    63 # l=l.append(me)
    64 # for k in l:
    65 #     print(l)

    python if、elif的区别

    elif配合if使用如果if中条件不成立执行elif,如果if条件成立,elif不会执行,注意即使上个if条件判断成立,它下面的if条件如果也成立依然会执行;

    a='zhanggen'
    
    if 'a' in a:
        print('ok1')
    
    elif 'h' in a:
        print('ok2')
    
    if 'g' in a:
        print('ok3')
    
    
    a='zhanggen'
    if 's' in a:
        print('ok1')
    
    elif 'h' in a:
        print('ok2')
    
    elif 'g' in a:  #只会执行1个
        print('ok3')
  • 相关阅读:
    TypeScript--变量
    TypeScript--Hello
    前端跨域的方式
    内存泄漏与垃圾回收机制
    前端拷贝
    React生命周期16版本
    Redux三大原则
    IE6常见CSS解析Bug及hack
    Redux的应用
    vue-router-基础
  • 原文地址:https://www.cnblogs.com/sss4/p/6641610.html
Copyright © 2011-2022 走看看