购物车程序实现

 题目来源:老男孩教育Python视频

#  -*- coding: utf-8 -*-
"""
@Author:yhf1995
@time:2020/1/30 8:24
"""
salary = input("Please input your money number:")
product_list =[
    ("Apple watch", 4500),
    ("milk", 60),
    ("bike", 1000),
    ("Starbucks", 40),
    ("book",80)
]
shopping_list = []
if salary.isdigit():
    salary = int(salary)
# 判断输入的金额是否为数字,如果是将其转为int类型,如果不是,输出 输入有误
    while True:
        for item in product_list:
            print(product_list.index(item),item)
            # 打印商品列表
        choice_number = input("Please enter the number of the product you want:>>>")
        if choice_number.isdigit():
            choice_number = int (choice_number)
            # 判断输入的商品编号是否为数字,如果是转为int型,如果不是,提示输入有误
            if choice_number < len(product_list) and choice_number >= 0:
                # 判断商品是否存在
                if salary >= product_list[choice_number][1]:
                    shopping_list.append(product_list[choice_number])
                    salary -= product_list[choice_number][1]
                    print("add shop %s in your shopping cart,and your salary is \033[31;1m%s\033[0m!"%(product_list[choice_number],salary))
                    # 判断余额是否足够,余额足够,将商品放入购物车,余额减去该商品价格,输出放入购物车的商品和余额
                else:
                    print("You have insufficient balance!")
                    # 余额不足
            else:
                print("product_number does not exist!")
                # 商品不存在
        elif choice_number == "Q":
            print("The shopping cart list is as follows:")
            for shop_item in shopping_list:
                print(shop_item)
            print("Your current balance is %s."%(salary))
            exit()
            # 输入Q 打印购物车商品和余额,并退出购物车程序
        else:
            print("The product_number is error!")
            # 商品编号有误
else:
    print("The amount entered is not a number!")
    # 金额有误

 这是学习老男孩教育Python课程的一个记录,我觉得该课程讲的挺好的。可参考金角大王Python之路02

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值