libra-gyf

GIthub base 04 购物车

""" 04 要求用户输入总资产,例如: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:
    for j in range(len(goods)):
        print('{} 号商品的信息为{}'.format(j, goods[j]))
    while True:
        salary = input('请输入您的总资产')
        if salary.isdigit():
            salary = int(salary)
            while True:
                chose = input("输入你想要的商品按'b'退出,退出")
                if chose == 'b':
                    exit('欢迎您下次再来')
                if chose.isdigit():
                    chose = int(chose)
                    if chose >=0 and chose <len(goods):
                        balance = salary - goods[chose]['price']
                        salary = balance
                        print('您的余额为:', balance)
                    while salary < 0:
                        c = input("您的余额已不足请及时充值,充值请按'c'号,退出请按'b'")
                        if c == 'c':
                            kk = input("输入您想冲值的金额")
                            balance += int(kk)
                            salary = balance
                            print('您的当前余额为',balance)
                            break
                        if salary < 0:
                            exit('您的余额不足')
                        if c == 'b':
                            exit('欢迎您下次再来')
                    else:
                        print('请输入正确的商品编号')
            else:
                print('请输入数字')

posted on 2020-01-10 19:15  libra-gyf  阅读(199)  评论(0编辑  收藏  举报

导航