栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python List练习题 购物车

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python List练习题 购物车

Python 练习题 实现一个类似购物车的语句,用户循环输入编号可以将列表内的商品储存到一个新的列表里面,当用户输入’q’时退出循环
products = [["iphone", 6888], ["MacPro", 14800], ["小米", 2499], ["Coffee", 31]]

flag = True
arr = []
while flag:
    print("---------商品列表---------")
    for index, item in enumerate(products):
        print(f"{index} t {item[0]} t {item[1]}")
    chose = input("请输入要选择的商品列表序号:")
    if not chose.isdigit() and chose != "q":
        print("请输入正确的商品列表序号:")
    elif chose.isdigit() and 0 <= int(chose) < len(products):
        arr.append(chose)
    elif chose == "q":
        flag = False
        total = 0
        print("您的购物清单:")
        for index, item in enumerate(products):
            if str(index) in arr:
                print(f"{index} t {item[0]} t {item[1]}")
                total += item[1]
        print(total)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/276190.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号