def buy_goods(name, price, amount): global shopping_car count = int(input(f'请输入您要购买{name}的数量:')) amount += count shopping_car[name][1] = amount shopping_car[name][2] = amount * price return amount 然后在主程序中调用这个函数即可: 复制 shopping_car = {'电脑': [1999, 0, 0], '鼠标': [10, 0, 0], '美女': [50, 0, 0], '火箭': [20000000, 0, 0]} goods = ['电脑', '鼠标', '美女', '火箭'] for i in range(len(goods)): print(i + 1, goods[i]) num = int(input('请输入您要购买的商品序号:')) if num == 1: a = buy_goods('电脑', 1999, a) elif num == 2: c = buy_goods('鼠标', 10, c) elif num == 3: e = buy_goods('美女', 50, e) elif num == 4: g = buy_goods('火箭', 20000000, g),继续写一段删除购物车的代码
时间: 2023-06-25 11:03:52 浏览: 155
mempool_hook_userdef_0924
可以在主程序中定义一个函数来删除购物车中的商品,具体实现如下:
```
def delete_goods(name):
global shopping_car
if name in shopping_car.keys():
shopping_car[name][1] = 0
shopping_car[name][2] = 0
print(f'{name}已从购物车中删除。')
else:
print(f'购物车中没有{name}。')
```
然后在主程序中调用这个函数即可:
```
goods = ['电脑', '鼠标', '美女', '火箭']
for i in range(len(goods)):
print(i + 1, goods[i])
num = int(input('请输入您要购买的商品序号:'))
if num == 1:
a = buy_goods('电脑', 1999, a)
elif num == 2:
c = buy_goods('鼠标', 10, c)
elif num == 3:
e = buy_goods('美女', 50, e)
elif num == 4:
g = buy_goods('火箭', 20000000, g)
delete = input('是否删除购物车中的某个商品?(Y/N)')
if delete.upper() == 'Y':
name = input('请输入要删除的商品名称:')
delete_goods(name)
```
阅读全文