python用字典编写购物程序_用python编写购物程序(1)

1 #!/usr/bin/env python

2 #-*- coding:utf-8 -*-

3 #Author:James Tao

4

5

6 salary=int(input('请输入您的工资:'))7 list_of_goods=[['iphone',5800],['Mac Pro',12000],['Starbuck',31],['Bicycle',800]]8 print('支持购买的商品及价格:',list_of_goods)9 balance=salary10 goods_of_bought=[]11 goods_of_categorical={}12

13 judge=True14 while balance>0 andjudge:15

16 #打印出商品列表及编号

17 for i inrange(len(list_of_goods)):18 print('{goods}对应编号为:{n} \n'.format(goods=list_of_goods[i][0],n=i))19

20 number = int(input('请输入您要购买的商品编号:'))21 #计算余额

22 balance=balance-int(list_of_goods[number][1])23

24 #判断余额是否为0

25 if balance>0:26

27 #将购买的商品加入购物车

28 goods_of_bought.append(list_of_goods[number][0])29 quit1=input('继续购买?(Y?N):')30 if quit1=='N':31 judge=False32

33 else:34

35 #若余额小于0,将上一次购买的商品金额去除

36 balance = balance + int(list_of_goods[number][1])37 quit2=input('余额不足,是否退出?(Y/N):')38 if quit2=='Y':39 judge=False40

41 #判断是否购买了商品

42 if goods_of_bought: #如果列表为空等于False

43

44 #统计购买的商品种类

45 goods_of_set=set(goods_of_bought)46

47 #统计购买的商品数量并输出

48 for item ingoods_of_set:49 goods_of_categorical[item]=goods_of_bought.count(item)50 print('您购买的商品及数量为:',goods_of_categorical)51

52 else:53 print('您未购买任何商品')54

55 print('余额为:',balance)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值