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

简介:

1.基本需求

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

(1)让用户输入工资;

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

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

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

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




2.实现基本思路

    基本思路可如下所示:

wKioL1XyQXaQFd6HAAHEHjynTRI164.jpg

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




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,如需转载请自行联系原作者

相关文章
|
3天前
|
数据挖掘 数据处理 Python
【亮剑】如何在 Python 中以表格格式打印列表?
【4月更文挑战第30天】本文介绍了Python中以表格格式打印列表的三种方法:1) 使用字符串格式化,适用于简单场景;2) 使用prettytable库,适合需要更多格式化选项的情况;3) 使用pandas库,适用于处理大量数据和复杂分析。根据需求选择合适的方法来展示数据。
|
4天前
|
索引 Python
python【列表】增删改查
python【列表】增删改查
|
7天前
|
人工智能 数据库 开发者
Python中的atexit模块:优雅地处理程序退出
Python中的atexit模块:优雅地处理程序退出
8 3
|
8天前
|
API Python
[AIGC] Python列表([])和字典({})常用API介绍
[AIGC] Python列表([])和字典({})常用API介绍
|
8天前
|
存储 索引 Python
Python从入门到精通——1.3.1练习编写简单程序
Python从入门到精通——1.3.1练习编写简单程序
|
8天前
|
机器学习/深度学习 存储 数据挖掘
Python从入门到精通——学习基础语法和数据类型 1.2.1变量、整数、浮点数、字符串、布尔值、列表、元组、字典和集合。
Python从入门到精通——学习基础语法和数据类型 1.2.1变量、整数、浮点数、字符串、布尔值、列表、元组、字典和集合。
|
9天前
|
算法 数据挖掘 数据处理
使用 Python 循环创建多个列表
在Python中,动态创建多个列表对于数据处理和算法实现十分有用。本文介绍了四种方法:1) 列表推导式,如创建偶数和奇数列表;2) 使用循环和`append()`,示例为生成斐波那契数列;3) 结合字典与循环,按条件(如正负数)分组;4) 列表生成器,用于一次性生成多组随机数列表。这些方法有助于提高代码效率和可读性。
20 1
|
9天前
|
存储 Python
Python 基于列表实现的通讯录管理系统(有完整源码)
Python 基于列表实现的通讯录管理系统(有完整源码)
7 0
|
14天前
|
数据采集 JavaScript 前端开发
使用Python打造爬虫程序之破茧而出:Python爬虫遭遇反爬虫机制及应对策略
【4月更文挑战第19天】本文探讨了Python爬虫应对反爬虫机制的策略。常见的反爬虫机制包括User-Agent检测、IP限制、动态加载内容、验证码验证和Cookie跟踪。应对策略包括设置合理User-Agent、使用代理IP、处理动态加载内容、验证码识别及维护Cookie。此外,还提到高级策略如降低请求频率、模拟人类行为、分布式爬虫和学习网站规则。开发者需不断学习新策略,同时遵守规则和法律法规,确保爬虫的稳定性和合法性。
|
14天前
|
数据采集 监控 前端开发
使用Python打造爬虫程序之入门探秘:掌握HTTP请求,开启你的数据抓取之旅
【4月更文挑战第19天】本文介绍了爬虫技术的基本概念和用途,阐述了HTTP协议的重要性。在Python中,借助requests库可轻松发送HTTP请求,如GET和POST。文章还展示了如何设置请求头、处理cookies和session。通过学习这些基础知识,读者将能够开始网络数据抓取,为进一步的数据分析奠定基础。后续文章将探讨HTML解析、动态内容处理及反爬虫策略。