您的位置:首页 > 编程语言 > Python开发

Python购物车

2020-03-31 19:44 836 查看

第一个Python小程序,购物车

'''
购物车
'''
li = [
{'name': '苹果', 'price': 10},
{'name': '香蕉', 'price': 20},
{'name': '西瓜', 'price': 30}
] #定义一个水果的元组
#把货物放在货架上
shopping_car = {}  #定义一个购物车的集合
print('欢迎来到水果店')
money = input('让我看看你的钱')
if money.isdigit() and int(money) > 0:  #判断money是数字并且>0
while(1):
for i,k in enumerate(li):
print('序号{},商品{},价格{}'.format(i,k['name'],k['price']))
choose = input('请输入序号')
if choose.isdigit() and int(choose) < len(li):
num = input('您想要购买的商品数量')
if num.isdigit():
if int(money) > li[int(choose)]['price'] * int(num):
money = int(money) - li[int(choose)]['price'] * int(num)
if li[int(choose)]['name'] in shopping_car:
shopping_car[li[int(choose)]['name']] = shopping_car[li[int(choose)]['name']] + int(num)
else:
shopping_car[li[int(choose)]['name']] = int(num)
print('购物车中有{},您的余额为{}'.format(shopping_car,money))
else:
print('金额不足')
break

else:
print('输入正确序号')
  • 点赞
  • 收藏
  • 分享
  • 文章举报
qq_37839947 发布了1 篇原创文章 · 获赞 0 · 访问量 46 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: