Python实现购物系统(示例讲解)


Posted in Python onSeptember 13, 2017

要求:

用户入口

1、商品信息存在文件里
2、已购商品,余额记录。

商家入口

可以添加商品,修改商品价格

Code:

商家入口:

# Author:P J J

import os

ps = '''
1 >>>>>> 修改商品
2 >>>>>> 添加商品
按q为退出程序
'''

# 打开两个文件,f文件为原来存取商品文件,f_new文件为修改后的商品文件
f = open('commodit', 'r', encoding='utf-8')
f_new = open('commodit_update', 'w+', encoding='utf-8')
file_list = f.readlines()

# 打印商品信息
while True:
 productslist = []
 # 从商品文件中读取出来的数据存放到productslist列表里
 for line in file_list:
  productname = line.strip().split()
  productname, oldprice = line.strip("\n").split()
  productslist.append([productname, int(oldprice)])
 choose = input("%s请选择:" %ps)
 if choose =='1':
  for index, item in enumerate(productslist):
   print(index, item)
  productindex = input("请输入要修改价格的商品序号:")
  if productindex.isdigit():
   productindex = int(productindex)
  while True:
   print('要修改商品信息:', productslist[productindex])
   price = input("请输入要修改的价格:")
   if price.isdigit():
    price = int(price)
    productslist[productindex][1]=price
    break
   else:
    print("请正确的输入价格!")
    continue

  #已经修改好的商品列表循环写入f_new文件夹

  for products in productslist:
   insert_data = "%s %s" %(products[0],products[1])
   f_new.write(insert_data+'\n')
  print("商品价格已经修改!")
  # 替换原来的文件
  f_new = open('commodit_update', 'r', encoding='utf-8')
  data = f_new.readlines()
  f = open('commodit', 'w+', encoding='utf-8')
  for line in data:
   f.write(line)
  f.close()
  f_new.close()
  #删除替换文件
  os.remove('commodit_update')
 elif choose =='2':
  # 添加商品
  f = open('commodit', 'a+', encoding='utf-8')
  pricename = input("请输入商品名:")
  while True:
   price = input("请输入商品价格:")
   if price.isdigit():
    f.writelines('%s %s\n' % (pricename, price))
    break
   else:
    print('输入错误请重新输入!')
    continue
  f.close()
  continue
 elif choose =='q':
  break
 else:
  print("输入错误请重新输入")
  continue

买家入口:

# Author:P J J

productslist = []
f = open('commodit','r',encoding='utf-8')
for line in f:
 productname,price = line.strip('\n').split()
 productslist.append((productname,int(price)))

print(productslist)
shopping_list = []

salary = input("请输入你的现金:")
if salary.isdigit():
 salary = int(salary)
 while True:
  # for item in productslist:
  #  print(productslist.index(item),item)
  for index,item in enumerate(productslist):
   print(index,item)
  #判断用户要输入
  user_choice = input("请选择要买什啥>>>:")
  if user_choice.isdigit():
   user_choice = int(user_choice)
   if user_choice < len(productslist) and user_choice >= 0:
    p_item = productslist[user_choice]
    if p_item[1] <= salary: #买得起
     shopping_list.append(p_item)
     salary -=p_item[1]
     print("加入 %s 购物车你的余额是\033[31;1m%s\033[0mRMB" %(p_item,salary))
    else:
     print("\033[32;1m 你的余额只剩[%s]RMB啦,还买个毛线\033[0m " %salary)
   else:
    print("\033[41;1m您输入的商品不存在,请重新输入!\033[0m")
  elif user_choice == 'q':
   print("----shopping_list----")
   for p in shopping_list:
    print(p)
   print("你的余额:\033[31;1m%s\033[0mRMB" %salary)
   #简单的余额记录
   f = open('salary','w+',encoding='utf-8')
   f.writelines(str(salary))
   f.close
   exit()
  else:
   print("错误选项")

操作流程:


Python实现购物系统(示例讲解)

我的目录:


Python实现购物系统(示例讲解)

1、新建一个文件,名为 commodit 商品排列格式如下(自己可以更改商品名字或者价格)

2、运行商家入口测试功能


Python实现购物系统(示例讲解)

我们输入1,首先测试修改商品:


Python实现购物系统(示例讲解)

输入0,修改第一个商品价格为400:


Python实现购物系统(示例讲解)

退出后查看 commodit 文件看见商品价格已经修改


Python实现购物系统(示例讲解)

--------------------------------------------------

测试添加商品:


Python实现购物系统(示例讲解)

查看 commodit文件


Python实现购物系统(示例讲解)

测试买家入口:


Python实现购物系统(示例讲解)

有钱了那就先来一台Iphone


Python实现购物系统(示例讲解)

再来60包炉石卡包


Python实现购物系统(示例讲解)

按q退出结账!并且有一个salary文件记录余额


Python实现购物系统(示例讲解)

此时目录会多一个salary文件


Python实现购物系统(示例讲解)

点开就能看到余额已经被记录

Python实现购物系统(示例讲解)

感想:

做完这个购物车花了2天,其实也不是整天都在弄,毕竟还要上课、学习。这次主要是熟悉文件的操作和一些基础知识的回顾,写完后能跑出功能就很开心了.因为中途遇到很多困难,解决了一个又出一个问题,不过通过上网查找和询问还是解决了。写完后感觉很low,毕竟自己敲得太少还是要多加练习,这个程序挺适合入门或者学完文件操作的亲来练练手。对了,自己测试程序的时候还出现bug,不过影响不是特别大,只是不要多次修改价格就行,这个问题我也想过怎么解决,就是把列表清空,这样数据就不会读出2遍,但又发现第二次读取的数据不是更改后的数据,我就在想,列表有没有刷新,清空功能。这里先留下这个问题吧。功能已经都实现了,但写的真的很low,等以后再掌握了新姿势,回头来改改!包括前面做的登录还有三级菜单!如果有跟我一样初学的可以一起学习Alex老师的python课程,如果有大神看到,并且能耐心看完,请大神再多指点指点小弟!

好了,Life is short,use python!

以上这篇Python实现购物系统(示例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python常见的格式化输出小结
Dec 15 Python
详解Python实现多进程异步事件驱动引擎
Aug 25 Python
python中文乱码不着急,先看懂字节和字符
Dec 20 Python
python如何在循环引用中管理内存
Mar 20 Python
Python实现ping指定IP的示例
Jun 04 Python
对Python 窗体(tkinter)文本编辑器(Text)详解
Oct 11 Python
对python 合并 累加两个dict的实例详解
Jan 21 Python
解决python super()调用多重继承函数的问题
Jun 26 Python
Python 面向对象之类class和对象基本用法示例
Feb 02 Python
Pytorch mask-rcnn 实现细节分享
Jun 24 Python
python中的django是做什么的
Jul 31 Python
python解压zip包中文乱码解决方法
Nov 27 Python
python模块之sys模块和序列化模块(实例讲解)
Sep 13 #Python
python模块之time模块(实例讲解)
Sep 13 #Python
python difflib模块示例讲解
Sep 13 #Python
Python网络编程 Python套接字编程
Sep 13 #Python
python和ruby,我选谁?
Sep 13 #Python
python实现简单点对点(p2p)聊天
Sep 13 #Python
django 常用orm操作详解
Sep 13 #Python
You might like
php select,radio和checkbox默认选择的实现方法
2010/05/15 PHP
jQuery+php简单实现全选删除的方法
2016/11/28 PHP
PHP遍历目录文件的常用方法小结
2017/02/03 PHP
PHP简单计算两个时间差的方法示例
2017/06/20 PHP
Laravel中的Blade模板引擎示例详解
2017/10/10 PHP
PHP 8新特性简介
2020/08/18 PHP
javascript 动态table添加colspan\rowspan 参数的方法
2009/07/25 Javascript
Javascript 鼠标移动上去 滑块跟随效果代码分享
2013/11/23 Javascript
获取select元素被选中的文本内容的js代码
2014/01/29 Javascript
JS获取地址栏参数的几种方法小结
2014/02/28 Javascript
JavaScript加入收藏夹功能(兼容IE、firefox、chrome)
2014/05/05 Javascript
js操作IE浏览器弹出浏览文件夹可以返回目录路径
2014/07/14 Javascript
javascript使用正则表达式检测IP地址
2014/12/03 Javascript
jQuery结合jQuery.cookie.js插件实现换肤功能示例
2017/10/14 jQuery
angularJs中$scope数据序列化的实例
2018/09/30 Javascript
jQuery使用$.extend(true,object1, object2);实现深拷贝对象的方法分析
2019/03/06 jQuery
webpack 代码分离优化快速指北
2019/05/18 Javascript
Python入门学习之字符串与比较运算符
2015/10/12 Python
python 常见字符串与函数的用法详解
2018/11/23 Python
Python绘制股票移动均线的实例
2019/08/24 Python
使用Python的Turtle库绘制森林的实例
2019/12/18 Python
检测tensorflow是否使用gpu进行计算的方式
2020/02/03 Python
python实现连连看游戏
2020/02/14 Python
乐天旅游台湾网站:Rakuten Travel TW
2017/06/01 全球购物
个性发展自我评价
2014/02/11 职场文书
优秀应届毕业生推荐信
2014/02/18 职场文书
有多年工作经验的自我评价
2014/03/02 职场文书
奥巴马竞选演讲稿
2014/05/15 职场文书
节约用水演讲稿
2014/05/21 职场文书
2014年采购部工作总结
2014/11/20 职场文书
解除劳动关系协议书2篇
2014/11/28 职场文书
教学督导岗位职责
2015/04/10 职场文书
赵氏孤儿观后感
2015/06/09 职场文书
2015年教师节感言
2015/08/03 职场文书
Pytorch 如何实现LSTM时间序列预测
2021/05/17 Python
能让Python提速超40倍的神器Cython详解
2021/06/24 Python