zoukankan      html  css  js  c++  java
  • python 实现购物车的优化

    功能要求:
    要求用户输入自己拥有总资产,例如:2000
    显示商品列表,让用户根据序号选择商品,加入购物车
    购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
    goods = [
    {"name": "电脑", "price": 1999},
    {"name": "鼠标", "price": 10},
    {"name": "游艇", "price": 20},
    {"name": "美女", "price": 998},
    ]'''
    goods = [
        {"name": "电脑", "price": 1999},
        {"name": "鼠标", "price": 10},
        {"name": "游艇", "price": 20},
        {"name": "美女", "price": 998},
    ]
    while True:
        name =input("输入用户名:")
        mima =input("请输入用户密码:")
        if name =="qi"and mima =="123":
            print("登陆成功")
            break
        else:
            print("用户名或密码错误")
    money = int(input("总资产"))
    lst=[]
    while 1:
        print('----------商品列表------------')
        for i,m in enumerate (goods):
            print(i,m)
        select =input("请选择您要购买的商品:")
        if select.isdigit():
            if 0<=int(select)<len(goods):
               s=goods[int(select)]
               a =s.get("price")
               print(a)
               if a >=money:
                   print("账户余额不足")
               else:
                   w = s.get("price")
                   moneys =money-w
                   print("余额还剩",moneys)
                   lst.append(s)
                   print(lst )
                   print("购买成功")
    
            else:
                print('选择有误,请重新选择')
    
        elif  select == 'q':
            print('--------------选中商品列表-------------')
            for i,v in enumerate(lst):
                print(i,v)
            break
     
  • 相关阅读:
    UVa 541 Error Correction
    UVa 11045 My T-shirt suits me
    【模板】Ford-Fulkerson算法
    POJ 1273 Drainage Ditches
    UVa 10158 War
    UVa 658 It's not a Bug, it's a Feature!
    【模板】并查集
    【模板】Floyd-Warshall算法
    UVa 10034 Freckles
    UVa 10048 Audiophobia
  • 原文地址:https://www.cnblogs.com/qiliuer/p/9438097.html
Copyright © 2011-2022 走看看