小破站看的教程 IT私塾 跟着做练习

products = [["iphone",6888],["MacPro",14800],["小米6",2499],["Coffee",31],["Book",60],["Nike",699]]
shop_car = []
print("------ 商品列表 ------")
i = 0
count = 0
for product in products:
print(i,product[0],product[1])
i+=1
while 1:
store = input("输入商品编号,添加对应商品到购物车 ")
if store == 'q':
break
elif int(store) >= 0 and int(store) <= 5:
shop_car.append(products[int(store)])
#print(products[int(store)])
count+=1
else:
print("无对应商品")
i = 0
for shop in shop_car:
print(i,shop[0],shop[1])
i+=1


中间变量没新建 都是直接操作参数 所以看上去不是很好 这里要区分好append 跟 extend 的用法 list整个添加是用append
这篇博客演示了如何使用Python创建一个商品列表和购物车。通过遍历商品列表并让用户输入商品编号来添加商品到购物车,实现了简单的购物车管理功能。文章重点在于理解和运用Python的列表操作,如append方法。
529

被折叠的 条评论
为什么被折叠?



