print('欢迎访问购物车')
money = int(input('为方便购物,请输入您的总资产:')) #输入金钱必须为数字类型
shopping_price_list = [{"name": "电脑", "price": 1000} ,
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 2000} ]
shopping_car = []
while True:
index = 0
for goods in shopping_price_list:
print(index, goods)
index += 1
choice = input('请选择序号: (如果退出请输入q)').strip().lower()
# 判断输入的是否为数字
if choice.isdigit():
choice = int(choice )
#判断商品存在
if choice >=0 and choice <len(shopping_price_list):
goods = shopping_price_list[choice]
count = int(input('请输入你要购买的数量:'))
#判断是否买得起
if goods['price']*count <= money:
goods.setdefault('count',count)
shopping_car.append(goods)
money -= goods['price']*count
print('目前购买商品为: '+ goods['name'] + ' 购买数量:'+ str(count) + ' 当前余额为:'+ str(money))
else:
money1 = goods['price']*count-money
print('资金不够,还差%s钱'%money1)
break
else:
print("商品不存在,请重新选择:")
elif choice == 'q':
print('----------已购买商品列表----------------')
for i in shopping_car:
print(i)
print('目前余额为:',money)
break
else:
print("无此选项")
-------------------------------------------------------------------------------------------------------------------------
更改输出颜色:
if goods['price']*count <= money:
goods.setdefault('count',count)
shopping_car.append(goods)
money -= goods['price']*count
print('