码迷,mamicode.com
首页 > 其他好文 > 详细

购物程序

时间:2017-07-24 01:18:39      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:int   序号   数字   span   dig   小程序   logs   pre   ges   

要求:

  1. 启动程序后,让用户输入工资,然后打印出带有序号的商品列表
  2. 用户输入商品序号购买相应的商品,或者输入 ‘ q ‘ 退出购买界面
  3. 选择商品后,检查余额是否足够,够则直接扣款,不够则提示余额不足
  4. 用户每购买一件商品后,或者输入 ‘ q ‘ 退出购买界面后,提示:是否继续购买?(Y/N),实现多次购买
  5. 若用户购买了商品,打印出购买的商品列表,总金额,余额;若用户没买任何商品,打印:交易结束,购物失败

Readme:

  运行程序,输入薪水,根据商品列表的序号选择购买的商品,可以选择多次购买,或者不购买

 

流程图:

 

技术分享

 

代码:

 1 # 简单的购物小程序
 2 
 3 product_list = [
 4     [surface pro 4, 7800],
 5     [dell xps 15, 12000],
 6     [macbook, 12000],
 7     [小米6, 2499],
 8     [iphone7, 4600],
 9     [坚果Pro, 1499]
10 ]
11 shopping_list = []
12 
13 
14 # 判断输入的薪水格式是否正确
15 while True:
16     salary = input(\n请输入您的薪水:)
17     if not salary.isdigit():                    # 薪水不是数字,结束循环
18         print(\n输入格式有误!请重新输入...)
19         continue
20     break
21 
22 
23 balance = salary = int(salary)
24 
25 print(\n-----------欢迎购买------------\n)
26 
27 # 生成带序号的商品列表
28 for index, item in enumerate(product_list):
29     print(index, item)
30 
31 
32 # 判断输入的序号是否符合要求
33 while True:
34 
35     while True:
36         i = input(\n输入您要购买的商品序号,或输入 q 取消购买:)
37         if i == q:                                 # 输入 q 退出购买界面
38             while True:
39                 a = input(\n是否继续购买?(Y/N):)
40                 if a != n and a != N and a != y and a != Y:
41                     print(\n输入格式有误,请重试...)
42                     continue
43                 elif a == y or a == Y:                  # 继续购买
44                     break
45                 else:                                       # 购买完毕
46                     if balance == salary:              # 没有买任何东西
47                         print(\n交易结束,购买失败...)
48                         exit()
49                     else:                              # 结算   
50                         print(\n您已成功购买以下商品:\n)
51                         for item in shopping_list:
52                             print(item)
53                         print(\n共消费金额 %d 元,余额 %d 元 % (salary - balance, balance))
54                         exit()
55             continue
56 
57         if not i.isdigit():                          # 序号不是数字,结束循环
58             print(\n输入格式有误!请重新输入...)
59             continue
60 
61         i = int(i)
62 
63         if i < 0 or i >= len(product_list):   # 序号范围不正确,结束循环
64             print(\n此商品不存在,请重新输入...)
65             continue
66         break
67 
68     product = product_list[i]
69     price = int(product[1])
70 
71     # 判断余额是否充足,够就直接扣款,不够提醒
72     if price <= balance:
73         balance -= price
74         shopping_list.append(product_list[i])
75         print(\n您已成功购买 %s ,当前余额为 %d 元 %(product, balance))
76     else:
77         print(\n购买失败,您的余额不足...)
78 
79     while True:
80         a = input(\n是否继续购买?(Y/N):)
81         if a != n and a != N and a != y and a != Y:
82             print(\n输入格式有误,请重试...)
83             continue
84         break
85 
86     if a == Y or a == y:
87         continue
88     else:
89         break
90 
91 if balance == salary:
92     print(\n交易结束,购买失败...)
93     exit()
94 else:
95     print(\n您已成功购买以下商品:\n)
96     for item in shopping_list:
97         print(item)
98     print(\n共消费金额 %d 元,余额 %d 元 %(salary-balance, balance))
99     exit()

 

购物程序

标签:int   序号   数字   span   dig   小程序   logs   pre   ges   

原文地址:http://www.cnblogs.com/Dreamer-qiao/p/7226793.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!