本文实例讲述了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实现的购物车功能示例相关推荐

  1. python制作购物网站_django搭建简单购物网站(功能不完整)

    简介:自学django,从搭建简单的购物网站开始,网站的功能不完整,目前完成:用户注册,用户登录和注销,商品展示,商品详情,购物车(不完整,没创建模型,但是已完成表单获取和session记录,没什么大 ...

  2. python制作购物网站开题报告_购物网站的设计与实现开题报告

    学 院 专 业 姓 名 学号 报告日期 论文(设计)题目 购物网站的设计与实现 指导教师 论文 (设计) 起止时间 一. 论文(设计)研究背景与意义 背景 : 随着时代的发展, 科学技术的进步和网络的 ...

  3. python制作购物网站开题报告_网上购物网站的开题报告

    一 . 选题背景及研究意义 网上购物系统作为 B2BB2C ( Business to Customer 即企业对消费者) , C2C ( Customer to Customer 即消费者对消费者 ...

  4. python购物车典例_Python实现的购物车功能示例

    本文实例讲述了Python实现的购物车功能.分享给大家供大家参考,具体如下: 这里尝试用python实现简单的购物车程序... 基本要求: 用户输入工资,然后打印购物菜单 用户可以不断的购买商品,直到 ...

  5. python实验报告内容实现购物车系统_Python实现的购物车功能示例

    本文实例讲述了python实现的购物车功能.分享给大家供大家参考,具体如下: 这里尝试用python实现简单的购物车程序... 基本要求: 用户输入工资,然后打印购物菜单 用户可以不断的购买商品,直到 ...

  6. python开发购物网站_python实现简单购物商城

    本文为大家分享了购物商城小程序,供大家参考,具体内容如下 软件版本:python3.x 功能:实现简单购物商城 1.允许用户选择购买多少件 2.允许多用户登录,下一次登录后,继续按上次的余额继续购买 ...

  7. python制作购物网站开题报告_网上商城的设计与实现开题报告

    篇一:网上购物系统的开题报告 本科毕业论文(设计)开题报告 论 文 题 目: 网上购物系统的实现 学 院: 专 业 .班 级:计算机科学与技术 学 生 姓 名: 指导教师(职称): 2013年 **月 ...

  8. python实现购物车总结,Python实现的购物车功能示例

    本文实例讲述了Python实现的购物车功能.分享给大家供大家参考,具体如下: 这里尝试用python实现简单的购物车程序... 基本要求: 用户输入工资,然后打印购物菜单 用户可以不断的购买商品,直到 ...

  9. 计算机毕业设计如何制作电子商务网站怎么制作购物网站计算机课程设计电子商城做什么(PHP-ASP.NET-c#-JavaWeb-SSM-SSH-J2EE-springBoot

    如果计算机毕业设计选题是<<电子商务网站>><<购物网站>>这样的题目,那么灵魂问答如下: 需要实现什么功能呢? 怎么样挑选适合自己的编程语言? 使用什 ...

最新文章

  1. python中True 为1 ,False为0
  2. Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之06.JDBC PreparedStatement
  3. Windows 8 的新文件搜索实例分享
  4. Quartz框架中的JobStore
  5. vista 中php4, php5 共存
  6. 1644E. Expand the Path
  7. ubuntu apache2 mysql_折腾 Ubuntu 系统续之安装PHP、Apache2和Mysql
  8. 【数字逻辑设计】组合电路
  9. MySQL---数据库从入门走向大神系列(二)-用Java对MySQL进行增删改查
  10. 数学建模美赛E题数据获取
  11. 语音芯片,语音合成芯片,嵌入式语音合成软件的区别
  12. c语言如何输出数组最大值和最小值,C语言输出数组中最大和次大的数
  13. 串口485接法图_rs485接口接线怎样操作?
  14. [WPF] 读取ini中中文字符乱码的解决
  15. IOI2017 Day1 Toy Train 题解
  16. vue 获取当前路由地址
  17. 知名油漆涂料品牌排行榜前十名
  18. 穿戴式设备应该新机遇--可以测心率的光感小米手环
  19. Ubuntu下使用GCC开发STM32的环境的搭建
  20. linux异步io 回调函数,Linux异步IO

热门文章

  1. java的(PO,VO,TO,BO,DAO,POJO)解释
  2. EXCEL如何验证重复数据?
  3. 国产奶粉冲击高端,飞鹤、蒙牛、合生元们的牌好不好打?
  4. noi linux硬盘启动,NOI Linux + Windows 10双系统(Win10引导)安装记录
  5. 华中科技大学c语言作业答案,华中科技大学标准C语言程序设计及应用习题答案...
  6. java开发变化_十年编程语言变化,大众程序员的路在哪里?
  7. mysql年份_【数据库_Mysql】查询当前年份的sql
  8. BUUCTF(pwn)picoctf_2018_are you root
  9. 详细讲解python中的析构方法;
  10. python 基础教程:对 property 属性的讲解及用法