1.基本需求

编写一个购物小程序,要求实现如下功能:

(1)让用户输入工资;

(2)输出购物菜单及产品价格;

(3)计算用户是否可支付;

(4)输出用户剩余的钱,问用户是否继续购物,如果选择继续,则继续进行,否则退出程序;

(5)若钱不够,输出用户还需要工作多久才能买得起(这里暂不实现此功能)。


2.实现基本思路

基本思路可如下所示:

在编写程序的时候即以该思路为主线,具体细节下面再提及。


3.实现细节

基于友好用户界面的原则,实现的细节可总结如下:

(1)用户输入工资时,如果输入的是非数字或没有输入,会有提示再次输入,而不会异常退出;

(2)用户输入购买物品时,如果输入的是非商品索引或非quit退出时,会有提示再次输入,而不会异常退出;

(3)每添加商品到购物列表后,会提示用户当前所剩余的money;

(4)用户选择退出时,会打印用户的购物清单,同时输出用户剩余的money;

(5)总的原则是,程序在执行过程中不会有异常情况出现,即使用户输入非法字符。


4.实现代码与注释

基于上述需求的实现代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
import sys
market = [
    ['Xiaomi Phone'2400],
    ['Iphone'6000],
    ['Computer'3800],
    ['Ipad'1800],
    ['Core Python'69],
    ['Router'109]
]
shop_list = []    #The shopping list of the buyer.
while True:
  salary = raw_input('Please input your salary per month:').strip()
  if not salary.isdigit():
    print 'Please enter your salary.'
    continue
  salary = int(salary)
  print "Welcome to our market!You can buy something cool here, or you can enter 'quit' to left.\n"
  break
#Upon:input the salary per month.输入工资同时避免异常情况。
while True:
  print 'The goods we serve are as follow:'
  for goods in market:
    print market.index(goods),goods[0],goods[1]
   
  #Upon:print the goods list.
  choice = raw_input('What do you want to buy?')
   
  if choice == 'quit':     #'quit' system
    print '\nYour shopping list are as follow:'
    for goods in shop_list:
      print goods[0],goods[1]
    print 'Now you have %d left.' % (salary)
    sys.exit('Goodbye!')
  elif len(choice) == 0:   #'enter' system
    continue
#实际上'luanma' system和'enter' system可以只用前者进行替代,这里只是想输出不同提示而已。
(即输入乱码时会提示,输入enter时不进行提示。) 
  if not choice.isdigit():  #'luanma' system,即如果用户输入非数字时,会有提示
    print 'Please input the right choice.(Number to buy things and quit to quit.)\n'
    continue
     
  #Upon: 'quit' system , 'enter' system and 'luanma' handle system.
   
  choice = int(choice)
  if choice >= len(market):
    print 'Could not find the item, try again!\n'
    continue   
  pri = market[choice]
  #Upon:To check if the number is legal.确认输入的数字是否在合法范围内
   
  pri = market[choice]
   
  if pri[1] <= salary:
    salary = salary - pri[1]  #The remaining money.
    shop_list.append(pri)     #Adding the goods to the list.
    print 'You have added %s to your shopping list, and you have %d left.\n' % (pri[0], salary)
  else:
    print '''You have %d left.
You can not afford to buy %s, but you can try to buy other things.\n''' % (salary, pri[0])

因为灵活用了列表作处理,所以相对来说代码不会太复杂。


4.测试

·对输入过程中可能出现的各种情况和说明如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day2$ python myshop.py 
Please input your salary per month:
Please enter your salary.    ===>直接输入'Enter'时会有提示
Please input your salary per month:klkdf
Please enter your salary.    ===>输入乱码时也会有提示
Please input your salary per month:10000
Welcome to our market!You can buy something cool here, or you can enter 'quit' to left.
The goods we serve are as follow:    ===>先打印了一个商品菜单
0 Xiaomi Phone 2400
1 Iphone 6000
2 Computer 3800
3 Ipad 1800
4 Core Python 69
5 Router 109
What do you want to buy?0    ===>正确输入商品索引号,会提示当前用户购物信息
You have added Xiaomi Phone to your shopping list, and you have 7600 left.
The goods we serve are as follow:
0 Xiaomi Phone 2400
1 Iphone 6000
2 Computer 3800
3 Ipad 1800
4 Core Python 69
5 Router 109
What do you want to buy?    ===>只输入'Enter'时,不会有提示输出,但会再次打印商品菜单
The goods we serve are as follow:
0 Xiaomi Phone 2400
1 Iphone 6000
2 Computer 3800
3 Ipad 1800
4 Core Python 69
5 Router 109
What do you want to buy?3
You have added Ipad to your shopping list, and you have 5800 left.
The goods we serve are as follow:
0 Xiaomi Phone 2400
1 Iphone 6000
2 Computer 3800
3 Ipad 1800
4 Core Python 69
5 Router 109
What do you want to buy?eqwer    ===>输入乱码时,会有提示,并接受再次输入
Please input the right choice.(Number to buy things and quit to quit.)
The goods we serve are as follow:
0 Xiaomi Phone 2400
1 Iphone 6000
2 Computer 3800
3 Ipad 1800
4 Core Python 69
5 Router 109
What do you want to buy?9    ===>输入不在合法范围内的数字时,也会有提示
Could not find the item, try again!
The goods we serve are as follow:
0 Xiaomi Phone 2400
1 Iphone 6000
2 Computer 3800
3 Ipad 1800
4 Core Python 69
5 Router 109
What do you want to buy?quit    ===>正常退出,会打印用户总的购物清单信息    
Your shopping list are as follow:
Xiaomi Phone 2400
Ipad 1800
Now you have 5800 left.
Goodbye!

相对来说,用户界面还是比较友好的,因为不会有太大的异常情况出现,同时也实现了基本的需求。

本文转自 xpleaf 51CTO博客,原文链接:http://blog.51cto.com/xpleaf/1693767,如需转载请自行联系原作者

【Python之旅】第二篇(三):基于列表处理的购物清单程序相关推荐

  1. Python开发【第二篇】:初识Python

    Python开发[第二篇]:初识Python Python简介 Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为 ...

  2. 计算机快捷键桌布,桌面改造 篇三:编程娱乐两不误 | 伪程序猿的Windows双屏组建/效率工具/桌面美化指南...

    桌面改造 篇三:编程娱乐两不误 | 伪程序猿的Windows双屏组建/效率工具/桌面美化指南 2020-07-10 11:41:39 153点赞 1107收藏 74评论 哈喽大家好,我是码呆茶!作为一 ...

  3. 初学Python——文件操作第二篇

    前言:为什么需要第二篇文件操作?因为第一篇的知识根本不足以支撑基本的需求.下面来一一分析. 一.Python文件操作的特点 首先来类比一下,作为高级编程语言的始祖,C语言如何对文件进行操作? 字符(串 ...

  4. Python与OpenCV(二)——基于背景差分法的运动目标检测程序分析

    背景差分法是传统运动目标检测算法中最常用的方法.其基本原理如图所示. 从图中可知,背景差分法是通过建立背景模型,比较当前帧与背景模型对应像素的差异点来检测运动目标的方法. 背景模型的建立主要通过两种方 ...

  5. 程序猿编程课堂 Python学习之入门篇1:环境搭建与第一个程序

    前言: Python作为目前比较热门的编程语言,其简单和简洁的语法使它成为一种非常好的通用编程语言,它是一种面向对象的动态类型语言,最初被设计用于编写自动化脚本(shell),并且随着版本的不断更新和 ...

  6. Python 学习日记第二篇 -- 列表,元组

    一.列表 列表是一个可以包含所以数据类型的对象的位置有序集合,它是可以改变的.    1.列表的序列操作(Python3) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...

  7. Python学习【第二篇】 : Python入门

    Python安装 Hello World 字符编码 变量 用户输入 模块初识 .pyc是个什么? 数据类型 数据运算 表达式if ...else语句 表达式for 循环 break and conti ...

  8. JB的Python之旅-爬虫篇--urllib和Beautiful Soup

    啃面包是辛苦的,那就开始学习爬虫吧,而学习爬虫的初衷很简单,爬图爬图,这就是学习的动力~ 1.爬虫信息了解 1)爬虫的定义: 先了解,什么叫爬虫,上度娘搜了一番,解释如下: 网络爬虫(又被称为网页蜘蛛 ...

  9. Python开发【第二篇】:基础数据类型

    内容概要 格式化输出 运算符 编码 基本数据类型 深浅拷贝.小数据池 1.格式化输出 # %s 占位字符串. 实际上可以占位任何东西(用的最多的) # %d 占位整数. 只能占位数字 # name = ...

最新文章

  1. 51nod1565 FFT
  2. 【开源】博客园文章编辑器4.0版发布
  3. ZStack中的编程技巧
  4. C语言再学习 -- dmesg 命令
  5. python敏感字替换_教学案例_Python处理敏感词汇方法
  6. 飞鸽传书2009绿色版 官方网站下载地址
  7. 札记 - PHP/JS/jQuery/MySQL/CSS/正则/Apache
  8. mysql数据库rpm包安装_Linux rpm包安装MySQL数据库问题总结
  9. WINDOWS XP优化批处理
  10. 工业机械臂直线插补相关记录
  11. mac外接显示器 竖屏 黑苹果_解决黑苹果HD3000核显 VGA和HDMI外接显示器无反应问题...
  12. php微信公众号发送邮件,GitHub - DongDavid/notify: 消息发送组件-邮件、微信公众号、企业微信、小程序...
  13. Android 强制指定录音声卡
  14. 手写VIO学习总结(二)
  15. C语言基础和语法知识
  16. Android webview 常见的优化方案
  17. 「备忘录」MacOS终端获取电脑硬件信息序列号UUID
  18. 互联网到底怎么连接的?一张图告诉你
  19. HDU 6194 后缀数组+单调栈
  20. STM32之中断的使用

热门文章

  1. java:lock锁
  2. oracle:小知识点
  3. 前端一HTML:三: 浏览器页面的本质-html,html语言规则
  4. lighttpd防御 Slow HTTP Denial of Service Attack 解决办法
  5. JS发送跨域Post请求出现两次请求的解决办法
  6. Visual Studio 2010 多定向的支持
  7. svn update -r m path 代码还原到某个版本(这样之前的log日志也就没了,也就是清空log日志)...
  8. [EMC++] Item 8. Prefer nullptr to 0 and NULL
  9. docker d盘_windows修改docker的默认存放位置
  10. Adobe Bridge 2021中文版