Python 完美购物车

项目需求:

1.商家添加一些商品信息
  2.当用户余额足够时,用户可以购买,余额自动减去对应的金额。当用户余额不足时,告知用户购买失败,并显示用户余额
  3.当用户完成购物时,打印购物车中商品信息
  4.用户可以输入c或check打印商品信息并继续消费
  5.用户可以输入q或quit退出程序,退出时,打印购物车中的商品信息并显示余额

代码


# 完美购物车程序练习

# 商品列表
product_list = [
    ('Iphone', 5888),
    ('Mac pro', 8000),
    ('XiaoMi', 299),
    ('coffice', 38),
    ('Bao Ma', 400000),
    ('Bike', 1999),
    ('cloth', 200)
]

# 购物车
shop_car = []

salary = input("Input your salary:")

# 判断输入是否是数字
if salary.isdigit():
    salary = int(salary)
else:
    exit("Invaild data type...")

msg = 'Welcome to Night_du Shopping mall'.center(50, '-')
print(msg)

# 设置退出条件,为真退出程序
flag = False
while flag is not True:
    print('prodect list'.center(50, '-'))
    for item in enumerate(product_list):
        index = item[0]
        p_name = item[1][0]
        p_price = item[1][1]
        print(index, ": ", p_name + "  ", p_price)
    user_choice = input('[q=quit,c=check]what do you want to buy?:')
    if user_choice.isdigit():  # 肯定是选择商品
        user_choice = int(user_choice)
        if user_choice < len(product_list):
            p_item = product_list[user_choice]
            if p_item[1] <= salary:  # 买得起
                shop_car.append(p_item)  # 放到购物车
                salary -= p_item[1]  # 减钱
                # \033[31;1m[%s]\033[0m  字体加颜色
                print("added [%s] into shop car,your current balance is \033[31;1m[%s]\033[0m" % (p_item, salary))
            else:
                print('Your balance is \033[31;1m[%s]\033[0m,cannot affords this...' % salary)
    else:
        if user_choice == 'q' or user_choice == 'quit':  # 判断输入的是否为q/quit
            print('purchased produce as below'.center(40, '-'))
            for item in shop_car:  # 购物车里面的商品
                print(item)
            print('END'.center(40, '-'))
            print("Your balance is \033[31;1m[%s]\033[0m" % salary)  # 余额
            print("Bye")
            flag = True  # 退出程序
        elif user_choice == 'c' or user_choice == 'check':  # 查看购物车中商品
            print('purchased produce as below'.center(40, '-'))
            for item in shop_car:
                print(item)
            print('END'.center(40, '-'))
            print("Your balance is \033[41;1m[%s]\033[0m" % salary)  # \033[41;1m[%s]\033[0m 字体加颜色/背景颜色

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值