手记

python 购物车

#! /usr/bin/env python# -*- coding:utf-8 -*-# -*- Author:Leon -*-"""功能要求:要求用户输入总资产,例如:2000显示商品列表,让用户根据序号选择商品,加入购物车购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。附加:可充值、某商品移除购物车"""# 代码实现如下:goods = [    {"name": "电脑", "price": 1999},    {"name": "鼠标", "price": 10},    {"name": "游艇", "price": 20},    {"name": "美女", "price": 998},]cart = []while True:    try:        assets_input = input("请输入总资产金额:")        assets = int(assets_input)        print("总资产为:",assets)        break    except:        print("输入错误... ...请重新输入 !")while True:    for k,v in enumerate(goods,1):            print(k,v['name'],v['price'])    try:        inp = input("请输入所需要购买商品的序号:")        inp_num = int(inp)        pass    # 判断输入正确性        if inp_num< len(goods)+1:            pass        else:            print("输入序号错误... ...请重新输入 !")            continue        inp_key = goods[inp_num-1]        print("************************购物车信息*************************")        print(inp_key['name'],inp_key['price'],"已加入购物车!")        print("购物车的商品如下列表:")        cart.append(inp_key)        expense = 0        for inp_key,inp_value in enumerate(cart,1):            print(inp_key,"[",inp_value['name'],inp_value['price'],"]")            p = inp_value['price']            expense = int(expense + p)        if assets > expense:            shengyu = assets - expense            print("还剩余:",shengyu)            print("提交购买请输入:y\n继续购买请输入:g\n清空购物车输入:c\n退出购物请输入:b\n更新购物车输入:w")            print("********************************************************")            userchoose = input("请提交选择:")            if userchoose == "y":                count = len(cart)                print("购买商品数量:",count)                print("总金额:",shengyu)                break            elif userchoose == "g":                continue            elif userchoose == "c":                del cart[:]                print("已清空购物车,商品列表显示:",cart)            elif userchoose == "w":                print("********************************************************")                print("购物车清单如下:")                for inp_key,inp_value in enumerate(cart,1):                    print(inp_key,"[",inp_value['name'],inp_value['price'],"]")                shopupdate= input("请选择要去掉的商品序号:")                update_num = int(shopupdate)                update_numkey =update_num -1                cart.remove(cart[update_numkey])                print("购物车更新清单如下:")                for inp_key,inp_value in enumerate(cart,1):                    print(inp_key,"[",inp_value['name'],inp_value['price'],"]")                count = len(cart)                print("购买商品数量:",count)                p = inp_value['price']                shengyu = assets - int(p)                print("还剩余:",shengyu)                print("********************************************************")            elif userchoose==None or userchoose!='y' or userchoose!='c'or userchoose!='g' or userchoose!='w':                print("输入非法数值,请重新输入... ...")                continue            else:                userchoose == "b"                break        else:            print("钱不够,请充值:")            assets_chongzhi = input("请输入充值金额:")            assets = assets + int(assets_chongzhi)            print("充值成功,总资产为:",assets)            print("********************************************************")    except:        print("输入序号错误... ...请重新输入 !")


0人推荐
随时随地看视频
慕课网APP