购物车

题目要求

功能要求:
要求用户输入自己拥有总资产,例如: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},
]
flag=1
while flag:
    money=input("输入你所拥有的资产(不小于0的整数):").replace(" ",'')
    flag=0
    if  money.isdigit() and int(money)>=0:
        money=int(money)
    else:
        flag=1
        print("你的输入有误,请重新输入")
sum=0
while 1:
    print("商品列表")
    for good in goods:
        print(goods.index(good) + 1, good["name"], good["price"])
    num=input("输入你要选择的商品序号(1到{}),输入q或Q退出:".format(len(goods))).replace(" ",'')
    if num.upper()=="Q":
        print("你已退出商城,已购买总价{}的商品".format(sum))
        break
    elif num.isdigit() and int(num)>0 and int(num)<=len(goods):
        num=int(num)
        if (sum+goods[num - 1]["price"])<=money:
            sum += goods[num - 1]["price"]
            print("你已成功购买价格{}的{}一份,已使用{}".format(goods[num-1]["price"],goods[num-1]["name"],sum))
        else:
            print("你已使用{},余额{}不足以购买价格为{}的{}号商品,请选择其他商品或者退出".format(sum,money-sum,goods[num-1]["price"],num))
    else:
        print("你输入的商品序号有误,请重新输入")

 新增一份带有购物篮的程序,可显示用户买的商品数量

goods = [
    {"name": "电脑", "price": 1999},
    {"name": "鼠标", "price": 10},
    {"name": "游艇", "price": 20},
    {"name": "美女", "price": 998},
]
while 1:#用户资产
    money=input("输入你所拥有的资产(不小于0的整数):").strip()
    flag=0
    if  money.isdigit() and int(money)>=0:
        money=int(money)
        break
    else:
        print("你的输入有误,请重新输入")
sum=0
shopping={}#购物篮,统计用户买的东西数量,可不写
while 1:
    print("商品列表")
    for good in goods:
        print(goods.index(good) + 1, good["name"], good["price"])#显示商品列表
    num=input("输入你要选择的商品序号(1到{}),输入q或Q退出:".format(len(goods))).strip()
    if num.upper()=="Q":
        print("你已退出商城,已购买总价{}的商品".format(sum))
        print("你购买的商品有:")
        for k in shopping.keys():
            print(k,"价格:{0} 数量:{1}".format(shopping[k]["价格"],shopping[k]["数量"]))
        break
    elif num.isdigit() and 0<int(num)<=len(goods):
        num=int(num)
        if (sum+goods[num - 1]["price"])<=money:
            sum += goods[num - 1]["price"]
            print("你已成功购买价格{}的{}一份,已使用{}".format(goods[num-1]["price"],goods[num-1]["name"],sum))
            if  goods[num-1]["name"] in shopping.keys(): #购物篮部分
                shopping[goods[num-1]["name"]]["数量"]+=1
            else:
                di={}
                di["价格"]=goods[num-1]["price"]
                di["数量"]=1
                print(di)
                shopping[goods[num-1]["name"]]=di
        else:
            print("你已使用{},余额{}不足以购买价格为{}的{}号商品,请选择其他商品或者退出".format(sum,money-sum,goods[num-1]["price"],num))
    else:
        print("你输入的商品序号有误,请重新输入")

 

新增一份money输入位小数也可以的
goods = [
    {"name": "电脑", "price": 1999},
    {"name": "鼠标", "price": 10},
    {"name": "游艇", "price": 20},
    {"name": "美女", "price": 998},
]
while 1:#用户资产
    money=input("输入你所拥有的资产(不小于0的数):").strip()
    if  money.replace(".","").isdigit() and money.count(".")<=1\
            and money.strip('.')==money and float(money)>=0:
        money=round(float(money),2)
        break
    else:
        print("你的输入有误,请重新输入")
sum=0
shopping={}#购物篮,统计用户买的东西数量,可不写
while 1:
    print("商品列表")
    for good in goods:
        print(goods.index(good) + 1, good["name"], good["price"])#显示商品列表
    num=input("输入你要选择的商品序号(1到{}),输入q或Q退出:".format(len(goods))).strip()
    if num.upper()=="Q":
        print("你已退出商城".format(sum))
        if sum==0:
            print("你未购买任何商品,余额{}".format(money))
        else:
            print("你购买的商品有:")
            for k in shopping.keys():
                print(k,"价格:{0} 数量:{1}".format(shopping[k]["价格"],shopping[k]["数量"]))
            print("总价为{},余额{}".format(sum,yue))
        break
    elif num.isdigit() and 0<int(num)<=len(goods):
        num=int(num)
        yue = round(money-sum,2)
        if (sum+goods[num - 1]["price"])<=money:
            sum += goods[num - 1]["price"]
            yue=round(money-sum,2)
            print("你已成功购买价格{}的{}一份".format(goods[num-1]["price"],goods[num-1]["name"]))
            if  goods[num-1]["name"] in shopping.keys(): #购物篮部分
                shopping[goods[num-1]["name"]]["数量"]+=1
            else:
                di={}
                di["价格"]=goods[num-1]["price"]
                di["数量"]=1
                shopping[goods[num-1]["name"]]=di
            print("你购买的商品有:")
            for k in shopping.keys():
                print(k, "价格:{0} 数量:{1}".format(shopping[k]["价格"], shopping[k]["数量"]))
            print("总价为{},余额{}".format(sum, yue))
        else:
            print("你已使用{},余额{}不足以购买价格为{}的{}号商品,请选择其他商品或者退出".format(sum,yue,goods[num-1]["price"],num))
    else:
        print("你输入的商品序号有误,请重新输入")

 



posted @ 2018-06-08 21:46  显示名称已经被使用  阅读(384)  评论(0)    收藏  举报