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 ...
随机推荐
- vue学习之-----组件递归调用
1.关键点 2.父组件 <template> <div> <div class="btn-title"> <el-button @clic ...
- axios和ajax对响应是文件流用blob处理
先看axios请求处理,下载文件 this.$axios.get(api.exportMortgageOrderExcelVisit, { params: params, responseType: ...
- spring@Validated校验用法
1.controller添加注解 public BaseResponse addOrUpdateUnit(@RequestBody @Validated RiskUnitDto riskUnitDto ...
- 16.SharedPreferences存储
1.SharedPreferences存储 不同于文件的存储方式,SharedPreferences是使用键值对的方式来存储数据的,保存为.xml文件. 也就是说当保存一条数据的时候,需要给这条数据提 ...
- Laravel 5.1 LTS 速查表
https://cs.laravel-china.org/# Artisan // 在版本 5.1.11 新添加,见 http://d.laravel-china.org/docs/5.1/autho ...
- Katalon-获取文本内容和预期文本对比(get Test)
- Mysql数据库的表结构
[INFORMATION_SCHEMA 数据库] 是MySQL自带的,它提供了访问数据库 元数据 的方式, 元数据:数据库名或表名,列的数据类型,或访问权限等. 在MySQL中,把[INFORMATI ...
- pytest设计项目结构
api目录封装所有的接口 testcases目录写所有的测试用例.conftest全局登录前置操作 data目录测试数据 common或者utils目录.存放公共模块比如读取yml文件.连接数据库.所 ...
- 详解 C++ 左值、右值、左值引用以及右值引用
一.左值和右值 1.左值 [可以取地址的对象就是左值] 左值是一个表示数据的表达式,比如:变量名.解引用的指针变量.一般地,我们可以获取它的地址和对它赋值,但被 const 修饰后的左值,不能给它赋值 ...
- 【文献阅读】Nonlinear controller design of a ship autopilot
(1)文章工作 The main goal here is to design a proper and efficient controller for a ship autopilot based ...