码迷,mamicode.com
首页 > 其他好文 > 详细

一个很low的购物车系统还花了我一下午时间

时间:2017-08-23 18:16:22      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:拷贝   index   new   购物   nbsp   改名   soj   丢失   list   

购物车的要求

有一两个入口

  用户入口:

    1.商品信息存在文件里

    2.已购商品余额记录

  商家入口

    2.可添加商品,修改商品

用户入口

俩个文件一个product.txt商品信息   一个历史购买数据

用户输入本想加一个判断历史购买是否由余额然后直接输入余额  ,但是if判断好像返回不了返回值

 1 # Author:Zhiyu Su
 2 
 3 shopping_list=[]
 4 f=open(product.txt,r,encoding=utf-8)
 5 product_list=eval(f.read())
 6 
 7 l = open(历史购买数据.txt, r, encoding=utf-8).read()
 8 
 9 
10 
11 salary = l
12 print(以前购物余额,salary)
13 
14 salary = input("input your salay")
15 if salary.isdigit():#字符串形式的数字返回为真
16     salary = int(salary)
17     while True:
18         for index,item in enumerate(product_list):#enumerate 取元素的下标
19 
20             #print(product_list.index(item),item)
21             print(index,item)
22         user_choice = input("选择》》》》》》》》")
23         if user_choice.isdigit():
24             user_choice=int(user_choice)
25             if user_choice < len(product_list) and user_choice >=0:
26                 p_item = product_list[user_choice]
27                 if p_item[1] <= salary:#买的起
28                     shopping_list.append(p_item)
29                     print(shopping_list)
30                     salary-= p_item[1]
31                     print("added%s into shopping cart your crent balance is \033[31;1m%s\033[0m" %(p_item,salary))  #字体红色 \033[31;1m**\033[0m
32                 else:
33                     print("\033[41;1m你的余额只剩[%s]啦,还买个毛线\033[0m"%salary)
34             else:
35                 print("不存在[%s]"% user_choice)
36         elif user_choice == q:
37             print("-----------shopping list--------------")
38             for p in shopping_list:
39                 print(p)
40             print("your current balance:",salary)
41             k = open(历史购买数据.txt, w, encoding=utf-8)
42             k.write(str(salary))
43             k.write(str(p))
44             k.close()
45             exit()
46         else:
47             print("sojdioj")
48 f.close()

商家入口

有待完善的地方:修改完毕之后保存两个文件一个是使用文档,一个是备份。文档以w模式写   而备份以追加模式写再加每次存写的日期 ,如果文档丢失按最接的时间拷贝到使用文本里(这里配备时间可能需要用到正则 )

 1 # Author:Zhiyu Su
 2 
 3 
 4 
 5 
 6 f =  open(product.txt,r,encoding=utf-8)
 7 
 8 
 9 data = eval(f.read())
10 f_new =  open(product.txt,w,encoding=utf-8)
11 # for i in data:
12 #     print(i)
13 
14 for index,item in enumerate(data):#enumerate 取元素的下标
15        print(index,item)
16 Choice = input(你要修改商品按x你要添加商品按t>>>>)
17 if Choice ==x:
18     choice = int(input("选择要修改的商品号》》》"))
19     if choice <len(data):
20         print(data[choice])
21         choice2 = input(修改名称按--m--还是修价格按--q-->>>)
22         if choice2 ==m:
23             choice3 = input("你要修改的商品名称>>>>>")
24             data[choice][0]=choice3
25             print(data)
26             f_new.write(str(data))
27         elif choice2 == q:
28             choice4 = input("你要修改的商品名称>>>>>")
29             data[choice][1] = choice4
30             print(data)
31             f_new.write(str(data))
32 elif Choice == t:       #添加功能
33     App = input(商品名称>>>>>>)
34     end = input(商品价格>>>>>>)
35     data.append([App,end])
36     f_new.write(str(data))
37 else:
38     print(兄弟只有t和x)
39 f.close()
40 f_new.close()

 










一个很low的购物车系统还花了我一下午时间

标签:拷贝   index   new   购物   nbsp   改名   soj   丢失   list   

原文地址:http://www.cnblogs.com/szy413227820/p/7419421.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!