zoukankan      html  css  js  c++  java
  • Python入门 Day2-购物车

    '''
    购物车
    功能要求:
    要求用户输入总资产,例如:2000
    显示商品列表,让用户根据序号选择商品,加入购物车
    购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
    goods = [{"name": "电脑", "price": 1999},
               {"name": "鼠标", "price": 10},
          {"name": "游艇", "price": 20},
          {"name": "美女", "price": 998},
    ]
    '''
    goods = [{"name": "电脑","price": 2000},
             {"name": "鼠标","price": 10},
             {"name": "游艇","price": 20},
             {"name": "美女","price": 1000},
    ]
    
    j = 1
    shopping_car = []
    count = 0
    total_price = 0
    Flag = True
    
    while True:
        money = (input("你要充值多少钱:")).strip()
        if money.isdigit():
            money = int(money)
            break
        else:
            print("请输入数字")
            continue
    
    while Flag:
        print('序号  待售商品 单价:')
        for i in goods:
            print(j,i['name'],i['price'])
            j += 1
        j = 1
        choice = (input('请输入你想购买的商品序号,输入C/c清空购物车,输入Y/y确认下单,输入Q/q退出购物车:'))
        if choice == 'Y' or choice == 'y':
            print('确认购买商品:',shopping_car)
            print('你的账户余额为:',money - total_price)
            Flag = False
        elif choice == 'C' or choice == 'c':
            shopping_car.clear()
            total_price = 0
            print('购物车已清空,请重新添加商品')
            print(' ')
            # continue
        elif choice == 'Q' or choice == 'q':
            break
        elif choice.isdigit():
            if 1 <= int(choice) <= len(goods):
                cot = int(input('请输入购买数量: ').strip())
                choice = int(choice)
                shopping_car.append({'name':goods[choice-1]['name'],'count':cot})  ## 将所选商品及数量添加到购物车
                # j = 1
                total_price += goods[choice-1]['price'] * cot
                print('商品总价: ',total_price)
                if money < total_price:
                    print('金额不足,无法添加')
                    print('                ')
                    shopping_car.remove({'name':goods[choice-1]['name'],'count':cot})
                    total_price -= goods[choice - 1]['price'] * cot
                else:
                    print('购物车已添加',shopping_car)
                    print('-----------------------------------')
                    print('                                   ')
        else:
            print('请按提示输入:')
  • 相关阅读:
    Tomcat压缩传输设置
    Spring事务的开启方式
    Transactional参数说明
    Java获取异常堆栈信息
    ElasticSearch的matchQuery与termQuery区别
    156-PHP strrpos和strripos函数
    155-PHP stripos函数
    154-PHP strpos函数
    153-PHP htmlentities函数
    152-PHP htmlspecialchars函数
  • 原文地址:https://www.cnblogs.com/wangph/p/8830913.html
Copyright © 2011-2022 走看看