购物车程序

本文介绍了一个简单的购物车程序流程,通过Python实现用户选择商品并从预设工资中扣除相应金额的功能。程序包括商品列表展示、用户输入处理、余额计算等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

购物车程序流程图:

购物车程序:
product_list=[('iphone',5800),('watch',12000),('bike',800),('book',120),('coffee',31), ('tea',50)]
shopping_list=[]

while True:
    salary=input("please input your salary:")
    if salary.isdigit():
        salary=int(salary)
        while True:
            for index,item in enumerate(product_list):
                print(index,item)
            choose=input("now you can choose your wanting!")
            if choose.isdigit():
                choose=int(choose)
                if choose<len(product_list) and choose>=0:
                    p_item=product_list[choose]
                    if salary>=p_item[1]:
                        shopping_list.append(p_item)
                        salary-=p_item[1]
                        print("added %s in your shopping cart and now your balance is %s"%(p_item,salary))
                    else:
                        print("your balance is not enough! now your balance is %s"%salary)
                else:
                    print("your choice is wrong!")
            elif choose=='q':
                print("looking forward your next coming!\n"
                      "-------shoppping list------\n"
                      "now your balance is%s"%salary)
                for p_item in shopping_list:
                   print(p_item)
                exit()
            else:
                print("your choose is wrong!you can choose others!")



    else:
         print("your salary is wrong! you can input it again!")

小结:
一、“for index,item in enumerate(product_list):”enumerate--取出商品下标。
二、“\033[31;1m%s\033[0m ”:加色强调。31--红色;32--绿色;41--红底;42--绿。
三、exit()退出
四、“.isdigit()”判断是否为数字。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值