侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

Python  /  管理员 发布于 7年前   114

要求:

用户入口

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

商家入口

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

Code:

商家入口:

# Author:P J Jimport osps = '''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 Jproductslist = []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("错误选项")

操作流程:


我的目录:


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

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



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




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



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




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

测试添加商品:



查看 commodit文件




测试买家入口:



有钱了那就先来一台Iphone



再来60包炉石卡包



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



此时目录会多一个salary文件



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

感想:

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

好了,Life is short,use python!

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


  • 上一条:
    python实现简单聊天应用 python群聊和点对点均实现
    下一条:
    python模块之sys模块和序列化模块(实例讲解)
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(95个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客