首页 > 代码库 > 购物车程序练习

购物车程序练习

1.开发一个购物车程序

要求:选定商品后需要减少相应的金额并将商品存入购物车

技术分享
salary=input("卡内金额:")
if salary.isdigit():
    salary = int(salary)
else:
    print("请输入正确的数据类型")

shop_list=[
    (苹果5S,4999),
    (美的空调,3000),
    (联想笔记本,6890),
    (小米电源,210),
    (乐视TV,5999),
    (华为荣耀8,3999),
    (荣耀7,2000),
    (苹果7PLUS,7000)
]
shop_car=[]
tag=False
while tag is not True:    #当循环遇到tag为True时停止循环
    print("商品清单列表".center(50,-))
    for item in enumerate(shop_list):
        index=item[0]
        i_name=item[1][0]
        i_price=item[1][1]
        print(index,i_name,i_price)
    user_select=input("[q=quit,c=check] 输入你要购买的商品编号:")

    if user_select.isdigit():
         user_select = int(user_select)
         if user_select < len(shop_list):
             p_item=shop_list[user_select]
             if p_item[1] <= salary:
                 shop_car.append(p_item[0])
                 salary -= p_item[1]
                 print("购买了商品\033[41;1m%s\033[0m,剩余金额\033[32;1m %s\033[0m"%(p_item[0],salary))
         else:
             print("余额不足")
    elif user_select == c or user_select == check:
        print("加入购物车的商品".center(50,*))
        print(shop_car)
    else:
        print("不买了")
        tag=True
View Code

 


测试运行:
技术分享

购物车程序练习