基本信息
源码名称:python购物车小程序(入门级例子)
源码大小:1.40KB
文件格式:.py
开发语言:Python
更新时间:2018-12-03
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

实现购物车功能例子

选择要买么?>>>>>:n
invalid option
0 ('iphoe', 5800)
1 ('Mac Pro', 9000)
2 ('Bike', 800)
3 ('Watch', 10500)
4 ('Coffee', 30)
5 ('book', 50)
选择要买么?>>>>>:q
--------shopping list------
('Mac Pro', 9000)
('book', 50)
Your current balance: 950

Process finished with exit code 0

product_list = [
    ('Iphone',6800),
    ('Mac Pro',9800),
    ('Bike',800),
    ('Watch',10000),
    ('Coffee',31),
    ('Python Book',120),
]
shopping_list = []
salary = input("Input your salary:")
if salary.isdigit():
    salary = int(salary)
    while True:
        for index,item in enumerate(product_list):
            #print(product_list.index(item),item)
            print(index,item)
        user_choice = input("选择要买嘛?>>>:")
        if user_choice.isdigit():
            user_choice = int(user_choice)
            if user_choice < len(product_list) and user_choice >=0:
                p_item = product_list[user_choice]
                if p_item[1] <= salary: #买的起
                    shopping_list.append(p_item)
                    salary -= p_item[1]
                    print("Added %s into shopping cart,your current balance is \033[31;1m%s\033[0m" %(p_item,salary) )
                else:
                    print("\033[41;1m你的余额只剩[%s]啦,还买个毛线\033[0m" % salary)
            else:
                print("product code [%s] is not exist!"% user_choice)
        elif user_choice == 'q':
            print("--------shopping list------")
            for p in shopping_list:
                print(p)
            print("Your current balance:",salary)
            exit()
        else:
            print("invalid option")