python-购物车程序

程序:购物车程序

需求:

启动程序后,让用户输入工资,然后打印商品列表
允许用户根据商品编号购买商品
用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
可随时退出,退出时,打印已购买商品和余额
'''


'''
while True:
    salary = int(input("请输入您的工资:"))
    info = 
    ____________________商品信息____________________
    1.IPhone 5000
    2.sanxing 4000
    3.IPad Pro 3000
    4.bluetooth 1000
    
    print(info)
    shopnumber = int(input("输入商品编号:"))
    shopcost = [5000, 4000, 3000, 1000]
    if salary >= shopcost[shopnumber]:
        print("你已购买此商品,已自动从你账户扣除")
    else:
        print("对不起,你的余额已不足!")
'''

product_list = [
    ("IPhone", 5000),
    ("mac pro", 8900),
    ("bick", 800),
    ("watch", 10600),
    ("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(index,item)
        '''
        for item in product_list:
            print(product_list.index(item),item)
            '''
        user_choice = input("input the number of your choice:")
        #输入的是商品序号
        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 your car,your courrent balance is \033[31;1m%s\033[0m"%(p_item,salary))
                else:#买不起
                    print("\033[41;1m你的余额还剩%s是,请充值!\033[0m" % salary)
            # 输入的商品不存在
            else:
                print("the product of you input is not exit,place input again!")
        #用户需要退出的操作
        elif user_choice == "q":
            for p in shopping_list:
                print(p)
            print("your courrent balance is :",salary)
            exit()
        else:
            print("input error, plase input again!")

猜你喜欢

转载自blog.csdn.net/liushuichengshang/article/details/78475382