设为首页 加入收藏

TOP

Python-Day3 购物系统(一)
2017-10-10 08:28:41 】 浏览:3004
Tags:Python-Day3 购物 系统
要求:
用户入口
1、商品信息存在文件里
2、已购商品,余额记录。

商家入口
可以添加商品,修改商品价格


Code:
商家入口:
 1 # Author:P J J
 2 
 3 import os
 4 
 5 ps = '''
 6 1 >>>>>> 修改商品
 7 2 >>>>>> 添加商品
 8 按q为退出程序
 9 '''
10 
11 # 打开两个文件,f文件为原来存取商品文件,f_new文件为修改后的商品文件
12 f = open('commodit', 'r', encoding='utf-8')
13 f_new = open('commodit_update', 'w+', encoding='utf-8')
14 file_list = f.readlines()
15 
16 # 打印商品信息
17 while True:
18     productslist = []
19     # 从商品文件中读取出来的数据存放到productslist列表里
20     for line in file_list:
21         productname = line.strip().split()
22         productname, oldprice = line.strip("\n").split()
23         productslist.append([productname, int(oldprice)])
24     choose = input("%s请选择:" %ps)
25     if choose =='1':
26         for index, item in enumerate(productslist):
27             print(index, item)
28         productindex = input("请输入要修改价格的商品序号:")
29         if productindex.isdigit():
30             productindex = int(productindex)
31         while True:
32             print('要修改商品信息:', productslist[productindex])
33             price = input("请输入要修改的价格:")
34             if price.isdigit():
35                 price = int(price)
36                 productslist[productindex][1]=price
37                 break
38             else:
39                 print("请正确的输入价格!")
40                 continue
41 
42         #已经修改好的商品列表循环写入f_new文件夹
43 
44         for products in productslist:
45             insert_data = "%s %s" %(products[0],products[1])
46             f_new.write(insert_data+'\n')
47         print("商品价格已经修改!")
48         # 替换原来的文件
49         f_new = open('commodit_update', 'r', encoding='utf-8')
50         data = f_new.readlines()
51         f = open('commodit', 'w+', encoding='utf-8')
52         for line in data:
53             f.write(line)
54         f.close()
55         f_new.close()
56         #删除替换文件
57         os.remove('commodit_update')
58     elif choose =='2':
59         # 添加商品
60         f = open('commodit', 'a+', encoding='utf-8')
61         pricename = input("请输入商品名:")
62         while True:
63             price = input("请输入商品价格:")
64             if price.isdigit():
65                 f.writelines('%s %s\n' % (pricename, price))
66                 break
67             else:
68                 print('输入错误请重新输入!')
69                 continue
70         f.close()
71         continue
72     elif choose =='q':
73         break
74     else:
75         print("输入错误请重新输入")
76         continue
买家入口:
 1 # Author:P J J
 2 
 3 productslist = []
 4 f = open('commodit','r',encoding='utf-8')
 5 for line in f:
 6     productname,price = line.strip('\n').split()
 7     productslist.append((productname,int(price)))
 8 
 9 print(productslist)
10 shopping_list = []
11 
12 salary = input("请输入你的现金:")
13 if salary.isdigit():
14     salary = int(salary)
15     while True:
16         # for item in productslist:
17         #     print(productslist.index(item),item)
18         for index,item in enumerate(productslist):
19             print(index,item)
20         #判断用户要输入
21         user_choice = input("请选择要买什啥>>>:")
22         if user_choice.isdigit():
23             user_choice = int(user_choice)
24             if user_choice < len(productslist) and user_choice >= 0:
25                 p_item = productslist[user_choice]
26                 if p_item[1] <= salary: #买得起
27                     shopping_list.append(p_item)
28                     salary -=p_item[1]
29                     print("加入 %s 购物车你的余额是\033[31;1m%s\033[0mRMB" %(p_item,salary))
30                 else:
31                     print("\033[32;1m 你的余额只剩[%s]RMB啦,还买个毛线\033[0m " %salary)
32             else:
33                 print("\033[41;1m您输入的商品不存在,请重新输入!\033[0m")
34         elif user_choice == 'q':
35             print("----shopping_list----")
36             for p in shopping_list:
37                 print(p)
38             print("你的余额:\033[31;1m%s\033[0mRMB" %salary)
39             #简单的余额记录
40             f = open('salary','w+',encoding='utf-8')
41             f.wr
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python-Day3 购物系统 下一篇Python文件操作---合并文本文件内..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目