Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4552
  • 博文数量: 3
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 30
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-27 13:51
文章分类
文章存档

2016年(3)

我的朋友
最近访客

分类: Python/Ruby

2016-06-27 14:02:55

程序要求:
1.用户启动程序时要求输入购物金额,并打印商品列表
2.允许用户不断的选择购买商品
3.购买时检测:余额是否足够--如果足够--直接扣款。如果不够--打印余额不足并退出
4.允许用户主动退出程序--退出时打印已购商品列表

小程序主要用到 列表、循环、判断、几个常用的方法 。

  1. #商品原列表
  2. shop_list = [
  3.     ('电脑',5000),
  4.     ('鼠标',50),
  5.     ('手机',6000),
  6.     ('显示器',2000),
  7.     ('显卡',5888),
  8.     ('香烟',100),
  9.     ('眼镜',70)
  10. ]
  11. shop_car = []
  12. #用户输入购买金额,并判断是否为数字
  13. cost = input("请输入圈入金额:")
  14. if not cost.isdigit() :
  15.     exit("输入错误,请输入数字")
  16. else:
  17.     cost = int(cost)
  18. print("商品列表".center(50,'-'))
  19. for shop_list_for in enumerate(shop_list):
  20.     shop_num_base = shop_list_for[0]
  21.     shop_num = shop_num_base +1 #商品编号
  22.     shop_name = shop_list_for[1][0] #商品名
  23.     shop_cost = shop_list_for[1][1] #商品金额
  24.     print(shop_num,'.',shop_name,shop_cost)
  25. for i in range(99):
  26.     user_num = input("请选择商品编号进行购买,Q=QUIT,C=CHECK: \n")
  27.     if user_num.isdigit():
  28.         user_num = int(user_num)-1
  29.         if user_num < len(shop_list):
  30.             if cost >= int(shop_list[user_num][1]):
  31.                 cost -= shop_list[user_num][1]
  32.                 shop_car.append(shop_list[user_num])
  33.                 print('购买成功,您的余额还剩余',cost)
  34.             else:
  35.                 print("当前余额",cost,"\n余额不足,本次购买商品如下\n",shop_car)
  36.                 exit()
  37.         else:
  38.             print('该商品编号不存在')
  39.     if user_num=="q":
  40.         print("欢迎您下次再来\n",shop_car)
  41.         exit()
  42.     if user_num=="c":
  43.         print("购物车列表".center(50,'-'),"\n")
  44.         for check_car in shop_car:
  45.             check_name = check_car[0]
  46.             check_cost = check_car[1]
  47.             print(check_name,"\t",check_cost,"\n")
  48.         print("----------当前余额:",cost,"¥----------")

阅读(274) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:Python解析json数据结构范例

给主人留下些什么吧!~~