python day03 自定义功能化的购物车 装饰器的使用
来源:互联网 发布:矩阵和向量相乘公式 编辑:程序博客网 时间:2024/06/05 05:45
如题,今天看视频后,偶然有感,打算用Python模拟一个自定义功能化的购物车,需求如下:
1 购物车提供两种检索功能:
@1全部商品信息——从指定文件中打印全部商品信息,供给用户挑选
@2首字母提示检索功能——提供首字母检索,检索到则打印对应商品信息
2 用户在这个购买平台,可以反复购买商品,购买后打印已经购买的商品信息,并提示余额
3 每次购买完毕,提示是否退出,选择退出则保存购物信息在文件中。
4 用装饰器给保存购物信息的函数添加两个功能:打印购物日期以及打印商品信息列表
废话不多说。上代码。如有错误,还请各位亲告知本小白。
首先给出商品信息的文件内容,文件名为goodmessage.txt:
goods:apple,price:10goods:beautifulgirls,price:999goods:iphone,price:4888goods:crow,price:499goods:TV,price:4999
以下便是全部代码了:
# -*- coding: utf-8 -*-"""Created on Sat Jul 22 16:06:02 2017@author: zh"""import timeclass pick_goods(): def __init__(self): self.shopping_goods={} #shopping_list用来存放提取的商品 self.salary=input("please input your salary:") #提示工资输出 self.enable=False #控制循环 self.kwargs={} #用来存放首字母索引的商品,这个字典的key为商品名的首字母,value为对应的商品名称 self.pick_list={} #用来从文件中读取商品信息 def read_goods_mess(self): #从商品文件中提取商品信息,(文件的格式为:goods:XXX,price:XXX) # 这里提取goods和price对应的值,然后存入pick_list这个字典中 with open("goodsmessage.txt","r") as f: for lines in f: key=lines.strip().split(",")[0].split(":")[1] value=lines.strip().split(",")[1].split(":")[1] self.pick_list[key]=value def salary_ifelse(self): #由于第二次循环时,字符输入型的数字salary已经转为数值型,将可能导致对后续if判定报错 #因此,我将这个处理单独提取出来,不放入循环,相当于salary预处理。 if self.salary.isdigit(): self.salary=int(self.salary) else: print("invalid salary input!") exit() def show_all_goods(self): #第一种模式下显示所用商品列表 print("welcome hole goods list".center(30,"-")) for k,v in self.pick_list.items(): print("goods:%s, price:%s" %(k,v)) print("-"*30) def shopping_list_1(self): #用来提示第一种模式后的商品选择 self.show_all_goods() choice2=input("choose your goods.....") if choice2 in self.pick_list: self.bought_goods(choice2) else: print("no such goods or spelling error!") def k_search(self): #将商品首字母提取出来存放在kwargs中,应用于第二种模式的首字母检索 for k in self.pick_list: self.kwargs[k[0]]=k def choose_index(self): #定义一个通过首字母检索goods的方法,检索到了就提示商品信息,包括价格,否则提示报错 choice1=input("choose 'a-z' to search your initial letter of goods:") if choice1.isalpha(): self.k_search() if choice1 in self.kwargs: temp=self.kwargs[choice1] print("goods:%s, price:%s" % (temp,self.pick_list[temp])) return temp else: print("no such goods!") else: print("invalid code!") def shopping_list_2(self,k): #第二种模式下,用户检索到商品后,提示是否确认选购 choose3=input("Are your sure buyying this:Y/N") if choose3=="Y"or choose3=="y": self.bought_goods(k) elif choose3=="N"or choose3=="n": pass else: print("invalid input!") def bought_goods(self,choice2): #两种模式选择后,用户确认选购商品 # 如果工资大于商品价格,开始打印购物信息,并将选购商品的次数记录下来 #否则,提示余额不足 if self.salary > int(self.pick_list[choice2]): self.salary-=int(self.pick_list[choice2]) print("goods:%s,price:%s added to your shopping list" %(choice2,self.pick_list[choice2])) print("your current balance:%s" % self.salary) self.shopping_count(choice2) else: print("current balance out of goods price!") def select_shopping(self): #用户完成第一次购物后,提示他是否继续选购,如果选择y就继续循环,否则给enable传递Fasle,使得其退出 choice4=input("Are you wantting to go on shoppong(Y) or exit(N)?") if choice4 in ['Y','y']: pass elif choice4 in ["N",'n']: self.enable=False else: print("invalid code!") def shopping_count(self,goodcode): #记录选购同一个商品的件数 if goodcode in self.shopping_goods: self.shopping_goods[goodcode]+=1 else: self.shopping_goods[goodcode]=1 def shopping_date(func): #装饰器一:save_shopping_list保存前,给其添加购物日期的功能 def wrapper(*args,**kwargs): with open("shoppinglist.txt", "a") as f: f.writelines("date:%s\n" % time.strftime("%Y-%m-%d %X")) func(*args,**kwargs) return wrapper def print_shopping_list(func1): #装饰器二:save_shopping_list保存完毕后,给其添加打印购物信息功能 def wrapper2(*args,**kwargs): func1(*args,**kwargs) with open("shoppinglist.txt", "r") as f: for lines in f: print(lines.strip()) return wrapper2 @shopping_date @print_shopping_list def save_shopping_list(self): #保存商品信息, with open("shoppinglist.txt","a") as fopen: fopen.writelines("shopping list".center(30, '-')) for x, y in self.shopping_goods.items(): fopen.writelines("\ngoods %s has bought %s" % (x,y)) fopen.writelines("\nyour current balance:%s\n" % self.salary) fopen.writelines("-"*30) fopen.writelines("\n") def start(self): choice=int(input("select a mode for logining in the pick_gods:\nshow all goods for 1,choose index searching for 2 >>>")) self.enable=True self.read_goods_mess() self.salary_ifelse() while self.enable: if choice==1: self.shopping_list_1() elif choice==2: k=self.choose_index() self.shopping_list_2(k) else: print("invalid code!") exit() self.select_shopping() self.save_shopping_list()if __name__=="__main__": p= pick_goods() p.start()运行效果如下:
C:\ProgramData\Anaconda3\python.exe E:/textpython/python_less/day3/picklist.py
please input your salary:10000
select a mode for logining in the pick_gods:
show all goods for 1,choose index searching for 2 >>>1
—welcome hole goods list—-
goods:apple, price:10
goods:beautifulgirls, price:999
goods:iphone, price:4888
goods:crow, price:499
goods:TV, price:4999
————————*
choose your goods…..iphone
goods:iphone,price:4888 added to your shopping list
your current balance:5112
Are you wantting to go on shoppong(Y) or exit(N)?n
date:2017-07-24 15:17:10
——–shopping list———
goods iphone has bought 1
your current balance:5112
—————————–*
“`
上面的横线上 *博客上来的,不知道怎么去除,运行应该不会有错~~~
- python day03 自定义功能化的购物车 装饰器的使用
- python 装饰器的使用
- python装饰器的使用
- python 装饰器的使用
- python装饰器的使用
- python装饰器的使用
- python——实现一个购物车的功能
- 23python装饰器的使用
- python 使用函数装饰器的例子
- Python中装饰器的使用
- python-函数装饰器的使用-25
- Python入门 装饰器的使用
- python函数装饰器的使用
- Python的装饰器
- python的装饰器
- python的装饰器
- Python的装饰器
- python的装饰器
- 使用浏览器访问azkaban报错:javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
- java高分局之jstat命令使用
- qt查找框设置
- 在迷茫中入行,一路前行,继续coding
- CentOS下RabbitMQ安装配置
- python day03 自定义功能化的购物车 装饰器的使用
- Oracle数据库误操作回滚到指定的时间点
- 菜鸟先飞之JAVA_内部类
- 百度-离线地图
- 一台机器上多账户管理——SSH Key切换解决push冲突
- cookie 中域的理解
- gulp学习指南
- Android 异步消息处理机制 让你深入理解 Looper、Handler、Message三者关系
- java String StringBuffer StringBudlider 区别