购物车程序

2021/7/29 17:06:06

本文主要是介绍购物车程序,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

名称:购物车程序
需求:
1. 启动程序后,输入用户当前余额,然后打印商品列表
2. 用户通过输入商品编号购买商品
3. 用户选择商品后,检查余额是否足够,足够就直接扣款,不够就提醒
4. 用户输入q 退出,退出后打印已购买商品和余额

# auth marcuya

#商品列表
product_list = [
    ('iphone12', 12000),
    ('mac book',17000),
    ('bycicle',800),
    ('model car',200),
    ('python book',150),
    ('erase',10),
]

#购物车列表
shopping_list =[]

#输入当前余额
balance = input("please input your current balance:")
if balance.isdigit():   #如果输入的是数字
    balance = int(balance)
    while True:
        for product in product_list: #打印商品列表
            print(product_list.index(product), product)

        product_index = input("please input the number of product whick you want:") #输入需要购买的商品编号
        if product_index.isdigit():
            product_index = int(product_index)
            if product_index >=0 and product_index <= len(product_list): #如果输入的编号在列表范围内
                balance = balance - product_list[product_index][1] #当前余额减去商品价格
                if balance >=0:
                    shopping_list.append(product_list[product_index][0]) #购物车列表添加选购的商品
                    print(" current balance is:\033[31;1m {}\033[0m".format(balance)) #打印当前余额
                    print("------- current shopping list ----------") #打印购物车商品列表
                    for item in shopping_list:
                        print(item)
                    print("----------------------------------------")
                else:
                    print("\033[41;1m current balance if ont enough!\033[0m") #余额不足
                    print("------- current shopping list ----------")
                    for item in shopping_list:
                        print(item)
                    print("----------------------------------------")
            else:
                break
        else:
            break

 



这篇关于购物车程序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程