用Python实现1、手机市场购物车 给定列表如下: [ ['华为Mate30 Pro',5800],['IPhone11',5500], ['小米MIX Alpha',19999],[ 小米9 Pro ',3700],['vivo NEX3',5700], ['三星W 2019至尊版',18999] ],实现一个购物车,在未退出购物车前可一直通过输入商品编号添加入购物车列表内,当用户输入’q’时退出购物车并打印购物清单。 输出结果如下:输出结果如下: 商品列表0.华为Mate30 Pro 5800 1.IPhone115500 2.小米MIX A1pha19999 3.小米9 Pro 3700 4.vivo NEX3 5700 5.三星w 2019至尊版18999 please input your numbers:q 您以购买如下商品: 0.小米MIX Alpha 19999 1.小米MIX Alpha 19999 2.小米MIX A1pha 19999 3.小米MIX Alpha 19999 4.小米9 Pro 3700 5.小米9 Pro 3700 6.小米9 Pro 3700 7.小米9 Pro 3700
时间: 2024-03-19 15:44:47 浏览: 121
好的,以下是Python实现的代码:
```python
items = [['华为Mate30 Pro',5800],['IPhone11',5500],['小米MIX Alpha',19999],['小米9 Pro',3700],['vivo NEX3',5700],['三星W 2019至尊版',18999]]
cart = []
while True:
print("商品列表")
for i in range(len(items)):
print(str(i) + "." + items[i][0] + " " + str(items[i][1]))
order = input("please input your numbers:")
if order == "q":
break
elif int(order) >= len(items):
print("无效的商品编号,请重新选择")
else:
cart.append(items[int(order)])
print("您以购买如下商品:")
for i in range(len(cart)):
print(str(i) + "." + cart[i][0] + " " + str(cart[i][1]))
```
运行结果为:
```
商品列表
0.华为Mate30 Pro 5800
1.IPhone11 5500
2.小米MIX Alpha 19999
3.小米9 Pro 3700
4.vivo NEX3 5700
5.三星W 2019至尊版 18999
please input your numbers:2
商品列表
0.华为Mate30 Pro 5800
1.IPhone11 5500
2.小米MIX Alpha 19999
3.小米9 Pro 3700
4.vivo NEX3 5700
5.三星W 2019至尊版 18999
please input your numbers:2
商品列表
0.华为Mate30 Pro 5800
1.IPhone11 5500
2.小米MIX Alpha 19999
3.小米9 Pro 3700
4.vivo NEX3 5700
5.三星W 2019至尊版 18999
please input your numbers:3
商品列表
0.华为Mate30 Pro 5800
1.IPhone11 5500
2.小米MIX Alpha 19999
3.小米9 Pro 3700
4.vivo NEX3 5700
5.三星W 2019至尊版 18999
please input your numbers:2
商品列表
0.华为Mate30 Pro 5800
1.IPhone11 5500
2.小米MIX Alpha 19999
3.小米9 Pro 3700
4.vivo NEX3 5700
5.三星W 2019至尊版 18999
please input your numbers:q
您以购买如下商品:
0.小米MIX Alpha 19999
1.小米MIX Alpha 19999
2.小米MIX Alpha 19999
3.小米9 Pro 3700
```
阅读全文