一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

python实现购物车小程序代码示例

时间:2022-02-08 编辑:袖梨 来源:一聚教程网

本篇文章小编给大家分享一下python实现购物车小程序代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

功能实现:

(1)可以查看购物车的商品,和余额

(2)可以显示商品列表,根据商品的编号选择商品

#定义一个列表存放商品信息
products=[('iphone',5800),('bike',220),('vivo',2000),('book',20)]
shopping_list=[]#将购买的商品存在列表shopping_list 中
salary=input("输入你的工资")
#判断输入的工资是否是数字
if salary.isdigit():
    salary=int(salary)#把输入的工资转换为整型
    while True:
        # for i in products:输出商品编号和商品信息两种方式均可
        #     print(products.index(i),i)
        for index,i in enumerate(products):
            print(index,i)
        user_choice=input("选择要买的商品")
        #通过输入商品编号来选择商品
        if user_choice.isdigit():
            user_choice=int(user_choice)
            if user_choice>=0 and user_choice

效果:

热门栏目