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 相关文章推荐
Pyhthon中使用compileall模块编译源文件为pyc文件
Apr 28 Python
python开发之函数定义实例分析
Nov 12 Python
Python使用Selenium模块模拟浏览器抓取斗鱼直播间信息示例
Jul 18 Python
Python实现对字典分别按键(key)和值(value)进行排序的方法分析
Dec 19 Python
python装饰器常见使用方法分析
Jun 26 Python
在pytorch中为Module和Tensor指定GPU的例子
Aug 19 Python
如何在VSCode上轻松舒适的配置Python的方法步骤
Oct 28 Python
利用Python绘制Jazz网络图的例子
Nov 21 Python
PyCharm中如何直接使用Anaconda已安装的库
May 28 Python
python实现npy格式文件转换为txt文件操作
Jul 01 Python
python如何安装下载后的模块
Jul 03 Python
Python3 如何开启自带http服务
May 18 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
DC最新动画电影:《战争之子》为何内容偏激,毁了一个不错的漫画
2020/04/09 欧美动漫
php $_SERVER当前完整url的写法
2009/11/12 PHP
php常用文件操作函数汇总
2014/11/22 PHP
基于php判断客户端类型
2016/10/14 PHP
php-fpm重启导致的程序执行中断问题详解
2019/04/29 PHP
Laravel 5.5 异常处理 &amp; 错误日志的解决
2019/10/17 PHP
Yii 框架使用Forms操作详解
2020/05/18 PHP
基于jquery的仿百度搜索框效果代码
2011/04/11 Javascript
Js 回车换行处理的办法及replace方法应用
2013/01/24 Javascript
js为鼠标添加右击事件防止默认的右击菜单弹出
2013/07/29 Javascript
javascript文件中引用依赖的js文件的方法
2014/03/17 Javascript
JavaScript实现复制内容到粘贴板代码
2016/03/31 Javascript
分享JS数组求和与求最大值的方法
2016/08/11 Javascript
原生js实现查询天气小应用
2016/12/09 Javascript
详解vue父子组件间传值(props)
2017/06/29 Javascript
angularjs实现过滤并替换关键字小功能
2017/09/19 Javascript
JavaScript 高性能数组去重的方法
2018/09/20 Javascript
浅谈Vue为什么不能检测数组变动
2019/10/14 Javascript
关于Vue中$refs的探索浅析
2020/11/05 Javascript
python+mysql实现简单的web程序
2014/09/11 Python
python多重继承实例
2014/10/11 Python
win10系统中安装scrapy-1.1
2016/07/03 Python
Python使用matplotlib绘制多个图形单独显示的方法示例
2018/03/14 Python
用python编写第一个IDA插件的实例
2018/05/29 Python
Django 1.10以上版本 url 配置注意事项详解
2019/08/05 Python
Python 从attribute到property详解
2020/03/05 Python
python基于win32api实现键盘输入
2020/12/09 Python
综合素质的自我鉴定
2013/10/07 职场文书
关于雷锋的演讲稿
2014/05/10 职场文书
启动仪式策划方案
2014/06/14 职场文书
大学生工作自荐书
2014/06/16 职场文书
单位消防安全责任书
2014/07/23 职场文书
代理人委托书
2014/08/01 职场文书
教师学习群众路线心得体会
2014/11/04 职场文书
2014公司年终工作总结
2014/12/19 职场文书
python Tkinter模块使用方法详解
2022/04/07 Python