简单购物车程序(Python)

#简单购物车程序:
money_all=0
tag=True
shop_car=[]
shop_info={
'apple':10,
'tesla':100000,
'mac':3000,
'lenovo':30000,
'chicken':10,
}
while tag:
for k,v in shop_info.items():
print('商品名:{name}价格:{price}'.format(name=k,price=v))
goods=input('请输入你要买的商品:').strip()
if len(goods)==0 or goods not in shop_info:
continue
while tag:
if goods in shop_info:
count=input('请输入你要购买的数量:').strip()
if len(count)==0 or count.isdigit()==False:
print('输入有误,请重新输入')
continue
shop_car.append((goods,shop_info[goods],int(count)))#将商品名,价格,数量以元祖的形式保存到列表中

print('''
你的购物车如下:
%s

'''%(shop_car))
cmd =input('退出请输入N,其他键继续购买').strip()
if cmd == 'N' :
for item in shop_car:
_,x,y=item#取列表元祖中的后两个值
money_all+=int(x)*int(y)
print(
'''
**********你总共消费了************

********** %s 元 *************

**********欢迎下次光临**************

'''%(money_all)
)
tag=False
else:
break



运行效果如下:









原文地址:https://www.cnblogs.com/wxp5257/p/7216546.html