python实验报告内容实现购物车系统_Python实现的购物车功能示例
本文实例讲述了python实现的购物车功能。分享给大家供大家参考,具体如下:
这里尝试用python实现简单的购物车程序。。。
基本要求:
用户输入工资,然后打印购物菜单
用户可以不断的购买商品,直到余额不够为止
退出时打印用户已购买的商品和剩余金额。。。
代码:
#!/usr/env python
#coding:utf-8
import re,math
def get_customer_salary():
while true:
salary=raw_input('please input your monthly salary(a positive integer):')
if __is_valid_num(salary):
return int(salary)
else:
print '[warn] please input a valid number!'
def __is_valid_num(num):
p=re.compile(r'^\d+$')
m=p.match(num)
if m:
return true
else:
return false
def get_customer_selection():
while true:
selection=raw_input('please enter the goods number you want to buy:')
if __is_valid_num(selection):
if __is_a_valid_selection(int(selection)):
return int(selection)
else:
print '[warn] please enter a valid selection number'
else:
print '[warn] please enter a valid number!\n'
def __is_a_valid_selection(selection):
if 1<=selection<=get_total_amount_of_products():
return true
else:
return false
def get_products_list():
return {'flower':50,'perfume':300,'shoes':600,'clothing':800,'alcohol':300,
'makeup':800,'bike':1500,'car':200000,'apartment':5000000}
def get_total_amount_of_products():
return len(get_products_list())
def mapping_type_code_for_products():
return ['flower','perfume','shoes','clothing','alcohol','makeup','bike','car','apartment']
def get_product_price(type_code):
return get_products_list()[get_product_name(type_code)]
def get_product_name(type_code):
return mapping_type_code_for_products()[type_code-1]
def get_lowest_price_of_products():
price_list=[]
for k,v in get_products_list().items():
price_list.append(v)
return min(price_list)
def get_highest_price_of_produces():
price_list=[]
for k,v in get_products_list().items():
price_list.append(v)
return max(price_list)
def still_can_buy_something(left_money):
if left_money
return false
else:
return true
def still_want_to_buy_something():
while true:
answer=raw_input('do you still want to buy something?(y/n):')
result=is_a_valid_answer(answer)
if result=='yes':return true
if result=='no':return false
print '[warn] please enter [yes/no] or [y/n]!\n'
def is_a_valid_answer(answer):
yes_pattern=re.compile(r'^[yy][ee][ss]$|^[yy]$')
no_pattern=re.compile(r'^[nn][oo]$|^[nn]$')
if yes_pattern.match(answer):return 'yes'
if no_pattern.match(answer):return 'no'
return false
def show_shopping_list():
counter=1
for i in mapping_type_code_for_products():
print '''''(%d) %s: %s rmb''' % (counter,i+' '*(10-len(i)),str(get_products_list()[i]))
counter+=1
def is_affordable(left_money,product_price):
if left_money>=product_price:
return true
else:
return false
def time_needed_to_work_for_buying_products(salary,price):
result=float(price)/salary
return get_formatting_time(int(math.ceil(result)))
def get_formatting_time(months):
if months<12:return ('%d months' % months)
years=months/12
months=months%12
return ('%d years,%d months' % (years,months))
#主程序从这里开始
if __name__=='__main__':
salary=get_customer_salary() #获取月工资
total_money=salary
shopping_cart=[] #初始化购物车
while true:
show_shopping_list() #打印购物列表
#判断剩余资金是否能够购买列表中的最低商品
if still_can_buy_something(total_money):
selection=get_customer_selection() #获取用户需要购买的商品编号
product_price=get_product_price(selection)#获取商品的价格
product_name=get_product_name(selection)#获取商品的名称
if total_money>=product_price:
total_money-=product_price
#打印购买成功信息
print 'congratulations!you bought a %s successfully!\n' % product_name
shopping_cart.append(product_name)#将商品加入购物车
print 'you still have %d rmb left\n' % total_money #打印剩余资金
#判断是否还想购买其他商品
if not still_want_to_buy_something():
print 'thank you for coming!'
break
else:
#输出还需要工作多久才能购买
format_time=time_needed_to_work_for_buying_products(salary,product_price-total_money)
print 'sorry,you can not afford this product!\n'
print "you have to work '%s' to get it!\n" % format_time
#判断是否还想购买其他商品
if not still_want_to_buy_something():break
else:
print 'your balance is not enough and can not continue to buy anything.'
break
#打印购物车列表
print 'now,your balance is %d,and\nyou have buy %s' % (total_money,shopping_cart)
运行效果:
希望本文所述对大家python程序设计有所帮助。
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!
python实验报告内容实现购物车系统_Python实现的购物车功能示例相关推荐
- python实验报告内容实现购物车系统_Python 实践--购物车功能简单实现
实验环境: operating system: Centos 6.3 Python version:Python 2.6.6 知识点: Python 基本数据类型的常用操作 #!/usr/bin/en ...
- 计算机组成原理实验所用的指令,计算机组成原理实验报告-控制器及微指令系统的操作与运用...
计算机组成原理实验报告-控制器及微指令系统的操作与运用 (13页) 本资源提供全文预览,点击全文预览即可全文预览,如果喜欢文档就下载吧,查找使用更方便哦! 14.9 积分 成绩:实 验 报 告课程名称 ...
- 计算机基础数据库实验报告,北航计算机软件技术基础实验报告实验报告4-2——数据库应用系统的开发...
<北航计算机软件技术基础实验报告实验报告4-2--数据库应用系统的开发>由会员分享,可在线阅读,更多相关<北航计算机软件技术基础实验报告实验报告4-2--数据库应用系统的开发(10页 ...
- 设计一个小学生计算机辅助教学系统,c语言实验报告 小学生计算机辅助教学系统...
c语言实验报告 小学生计算机辅助教学系统 (11页) 本资源提供全文预览,点击全文预览即可全文预览,如果喜欢文档就下载吧,查找使用更方便哦! 14.90 积分 实验名称小学生计算机辅助教学系统学校学 ...
- 西邮计算机网络实验报告,西邮计算机网络实验报告内容模板-实验二-交换机基本配置...
<西邮计算机网络实验报告内容模板-实验二-交换机基本配置>由会员分享,可在线阅读,更多相关<西邮计算机网络实验报告内容模板-实验二-交换机基本配置(10页珍藏版)>请在人人文库 ...
- 计算机毕业设计Java学生实验报告管理系统(源码+系统+mysql数据库+lw文档)
计算机毕业设计Java学生实验报告管理系统(源码+系统+mysql数据库+lw文档) 计算机毕业设计Java学生实验报告管理系统(源码+系统+mysql数据库+lw文档) 本源码技术栈: 项目架构:B ...
- 计算机python实验报告_Python实验报告六:函数
安徽工程大学 Python程序设计 实验报告 班级:物流192 姓名:周立学号:3190505227 日期:2020年5月5日 指导老师:修宇 [实验目的] 掌握函数的定义与使用方法:掌握函数的参数传 ...
- python实验报告实验总结_Python词云制作实验报告
实验题目:n Python 词云制作实验报告 实验目的与要求: 础 掌握基础 Python 安装过程和词云制作. 实验步骤: n Python 安装 1.击 双击 Anaconda2-4.0.0-Wi ...
- python编程超市购物系统_python实现简单购物车系统(练习)
#!Anaconda/anaconda/python#coding: utf-8 #列表练习,实现简单购物车系统 product_lists= [('iphone',5000), ('computer ...
最新文章
- Fiddler无法抓取HTTPS的问题,Fiddler证书无法安装终极解决方案,
- mysql实例化_mysql多实例实例化数据库
- 分数加减法 http://acm.nyist.net/JudgeOnline/problem.php?pid=111
- 即时通讯学习笔记001---XMPP了解认知
- linux查询机器信息,linux_机器信息查询
- 随机森林的原理及Python代码实现
- Flask 应用最佳实践
- Python中容易忽视的知识点
- 两种消息模型: 点对点(queue)发布/订阅(topic)
- uniapp和原生开发区别_web app和原生app有什么区别?
- Android框架揭秘-Zygote笔记
- Matlab图例Legend多行排布、字体格式
- Windows Mobile 设备中心开启方法(win7/win10)
- 找不到系统指定路径的解决思路
- 创建列表、删除列表、查看列表长度、列表增加一个元素的几种方法
- yii之gii的使用
- 中国脑与语言认知领域研究学者
- 使用Metasploit对MSSQL渗透测试步骤——学习笔记
- 2022卡塔尔世界杯来了,谁是你心中的夺冠热门球队?
- 抖音seo源码二次开发,短视频seo源码二次开发