python菜鸟学习: 7. 购物车升级版,用户、商品信息存储,修改,新增
# -*- coding: utf-8 -*-
import os
'''
用户入口:
1. 商品信息存在文件里
2. 已购商品,余额记录
商家入口
1. 可以添加商品,修改商品价格
商品信息:
commdList.txt
华为手机,8888
iphon, 2222
bike, 8888
用户信息:
userinfo.txt
LiYuZhoupan,100000
ZhangSan,8888
liSi,10000
'''
# 读取商品信息,用户信息
def readCommod(filename):
commadList = {}
with open(filename, "r", encoding="utf-8") as f:
for i in f.readlines():
# 商品名称
name = i.split(",")[0]
info = i.split(",")[1].replace("\n", " ")
commadList[name] = info
f.close()
return commadList
# 追加商品信息,用户信息
def writeCommand(name, info, filename):
name = name
info = info
if str(info).isdigit():
with open(filename, 'a', encoding="utf-8") as f:
f.write(name + "," + str(info) + "\n")
else:
print("输入错误请重新输入!")
f.close()
# 修改商品信息
def changeCommand(name, info, filename):
name = name
info = info
if str(info).isdigit():
# 遍历原来的文件夹
with open(filename, 'r', encoding="utf-8") as f:
# 创建新的文件夹
with open(filename + "tmp", "a", encoding="utf-8") as f1:
# 将原来的文件内容添加到新的文件
for i in f.readlines():
# 如果传入的name存在原来文件中,则修改传入的info信息
if name == i.split(",")[0]:
f1.write(name + "," + str(info) + "\n")
else:
f1.write(i)
f1.close()
f.close()
os.remove(filename)
os.rename(filename + "tmp", filename)
# 购物车得到用户信息
def shoppingGetusreInfo():
# 输入用户名
username = input("请输入用户名:\n")
useSalary = 0
# 查找用户信息表中是否存在该用户,如果不存在则添加
userList = readCommod("userinfo.txt")
if username in userList:
print("您当前的余额为:{salary}".format(salary=userList[username]))
useSalary = userList[username]
else:
salary = input("your salary:\n")
if salary.isdigit():
writeCommand(username, salary, "userinfo.txt")
useSalary = salary
print("用户-->{_username}<--添加成功,余额为-->{_salary}<--".format(_username=username, _salary=useSalary))
else:
print("请输入整数")
changeCommand(username, useSalary, "userinfo.txt")
print("用户-->{_username}<--,余额为-->{_salary}<--".format(_username=username, _salary=useSalary))
return username, int(useSalary)
# 购物车挑选商品:
def shoppingGetCommand():
# 设置标签
flag = True
# 定义所选购物品
yourCommandList = []
username, money = shoppingGetusreInfo()
print("商品信息如下:".center(50, "#"))
# 将商品信息写入到字典中
commandDict = readCommod("commdList.txt")
# 将字段中的商品名称以数组存储
commandList = list(commandDict.keys())
for i in range(len(commandList)):
print(str(i) + "," + commandList[i] + "," + commandDict.get(commandList[i]))
print("请选择你需要的商品:".center(50, "*"))
while flag:
commandNum = input("商品编号,退出请按Q:\n")
if commandNum.isdigit() and int(commandNum) < len(commandList):
# 如果余额足够则添加商品,并扣除余额
if money > int(commandDict.get(commandList[int(commandNum)])):
yourCommandList.append(commandList[int(commandNum)])
money = money - int(commandDict.get(commandList[int(commandNum)]))
print("您选择的物品有{yourCommandList},余额为:{money}".format(yourCommandList=yourCommandList, money=money))
else:
print("您余额{money},不足够支付:{money1}".format(money=money,
money1=int(commandDict.get(commandList[int(commandNum)]))))
elif commandNum == "Q":
flag = False
else:
print("输入错误,请重新输入!")
# 打印最终购买的物品
print("您选择的物品有{yourCommandList},余额为:{money}".format(yourCommandList=yourCommandList, money=money))
# 回填余额信息
changeCommand(username, money, "userinfo.txt")
if __name__ == '__main__':
flag = input("请输入1/2,其他退出:【1,商家;2,顾客】:\n")
if flag.isdigit():
# 商家操作
if int(flag) == 1:
# 修改商品信息
# 将商品信息写入到字典中
commandDict = readCommod("commdList.txt")
# 将字段中的商品名称以数组存储
commandList = list(commandDict.keys())
for i in range(len(commandList)):
print(str(i) + "," + commandList[i] + "," + commandDict.get(commandList[i]))
name = input("请输入需要修改的商品名称:\n")
price = input("请输入需要修改的商品价格:\n")
# 如果存在商品字典中则修改商品价格
if name in commandDict:
changeCommand(name, price, "commdList.txt")
# 如果不存在商品字段中则增加商品信息
else:
writeCommand(name, price, "commdList.txt")
# 顾客操作
elif int(flag) == 2:
shoppingGetCommand()
else:
print("输入错误或者退出{flag}".format(flag=flag))
exit()
# writeCommand("liyuzhoupan123", "100000", "userinfo.txt")
# print(readCommod("userinfo.txt"))
# changeCommand("liyuzhoupan", "7777", "userinfo.txt")
# shoppingGetCommand()
python菜鸟学习: 7. 购物车升级版,用户、商品信息存储,修改,新增的更多相关文章
- python菜鸟学习心得
禁忌:学习没精力,就是没精打采.没有热情. 禁忌:学习一半,然后,放在一边. 禁忌:不要东一榔头,西一棒锤. 禁忌:学习要用心. 激情是动力,专注是效率 每次学习都是绕着网络转了一圈.还是要一步一个脚 ...
- head first python菜鸟学习笔记(第六章)
1. Python提供字典,允许有效组织数据,将数据与名关联,从而实现快速查找,而不是以数字关联. 字典是内置数据结构,允许将数据与键而不是数字关联.这样可以使内存中的数据与实际数据的结构保持一致.? ...
- head first python菜鸟学习笔记(第三章)
1.os.chdir()切换到指定目录下,os.getcwd(),得到当前目录. >>> import os>>> os.chdir('D:\\CodeDocume ...
- python通过字典实现购物车案例-用户端
import os dict01 = { 'iphone' : { '5999' : { '总部位于美国' : '价格相对较贵', }, }, 'wahaha' : { '15' : { '总部位于中 ...
- head first python菜鸟学习笔记(第七章) ——web应用之为数据建模
问题1. #意思是从athletelist.py中导入AthleteListfrom athletelist import AthleteList 源程序代码 import pickle from a ...
- head first python菜鸟学习笔记(第四章)
1,p124,错误:NameError: name 'print_lol' is not defined 要想文件内如图显示,需要把调用BIF print()改为调用第二章的nester模块中的pri ...
- 【Android菜鸟学习之路】环境搭建问题-修改AVD Path
更改avd默认路径
- Python学习--------------Atm+购物车系统
一.程序需求 模拟实现一个ATM + 购物商城程序: 1.额度 15000或自定义 2.实现购物商城,买东西加入 购物车,调用信用卡接口结账 3.可以提现,手续费5% 4.每月22号出账单,每月10号 ...
- python学习:购物车程序
购物车程序 product_list = [ ('mac',9000), ('kindle',800), ('tesla',900000), ('python book',105), ('bike', ...
- python入门学习:6.用户输入和while循环
python入门学习:6.用户输入和while循环 关键点:输入.while循环 6.1 函数input()工作原理6.2 while循环简介6.3 使用while循环处理字典和列表 6.1 函数in ...
随机推荐
- python json表格化输出
需求 将json数据以表格形式输出 超长文本换行输出 能显示中文 在linux终端输出 实现 首先数据的模样.既然是表格化输出,那必然传入的数据是一个数组(废话),如果一个项文本很长需要换行输出,那这 ...
- TP开发项目时遇到的问题记录
1.下载功能. TP自带Http下载类,使用时new一个就行,示例代码: public function download(){ //接收公文id $id = I('get.fid'); //根据公文 ...
- 《Makefile中变量的高级用法:变量的替换引用、变量的嵌套使用》
高级使用方法有两种:第一种是变量的替换引用,第二种是变量的嵌套引用. 第一种用法经常用到,第二种用法我们很少使用.我们应该尽量避免使用变量的嵌套引用,在必须使用时,嵌套的层数越少越好.因为这种方法表达 ...
- 《Makefile中DEP_LIBRARIES变量的作用》
1.例子 DEP_LIBRARIES := lib@InfoSDK_api lib@WMTMedia lib@WMTUtil lib@curl 该变量是Makefile里面内置的变量,作用是指定依赖的 ...
- satpy 处理卫星 FY4A 数据
读取数据并画图 import os import glob from datetime import datetime, timedelta from satpy.scene import Scene ...
- scrcpy不使用adb远程控制android
1.开启服务器 CLASSPATH=/data/local/tmp/scrcpy-server.jar app_process / com.genymobile.scrcpy.Server 1.23 ...
- IDEA EduTools Plugin Learning Cause
背景 编程培训需求,能够检测学生的输入内容与预期一致,有课程大纲 IDEA Plugin EduTools 是一个非常出色的培训工具,具备在IDE中学习,能够通过单元测试验证正确错误,能够设置用户输入 ...
- 【踩坑】lua加载模块但找不到模块最蠢的原因
这个问题比较蠢,我用MinGW编译的lua去加载了MSVC编译的lua模块导致找不到符号,然后花了几个小时找为什么我VS项目使用函数导出接口了但是函数依然没有导出(使用dumpbin和nm都能看到导出 ...
- wpf DataGrid cell 背景色修改参考
<DataTemplate.Triggers> <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource ...
- centos7下的apache2.4安全配置
基本概括 关键词Server ServerRoot "/etc/httpd" #apache软件安装的位置 Listen 80 #监听的端口号 ServerName ww ...