# -*- 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. 购物车升级版,用户、商品信息存储,修改,新增的更多相关文章

  1. python菜鸟学习心得

    禁忌:学习没精力,就是没精打采.没有热情. 禁忌:学习一半,然后,放在一边. 禁忌:不要东一榔头,西一棒锤. 禁忌:学习要用心. 激情是动力,专注是效率 每次学习都是绕着网络转了一圈.还是要一步一个脚 ...

  2. head first python菜鸟学习笔记(第六章)

    1. Python提供字典,允许有效组织数据,将数据与名关联,从而实现快速查找,而不是以数字关联. 字典是内置数据结构,允许将数据与键而不是数字关联.这样可以使内存中的数据与实际数据的结构保持一致.? ...

  3. head first python菜鸟学习笔记(第三章)

    1.os.chdir()切换到指定目录下,os.getcwd(),得到当前目录. >>> import os>>> os.chdir('D:\\CodeDocume ...

  4. python通过字典实现购物车案例-用户端

    import os dict01 = { 'iphone' : { '5999' : { '总部位于美国' : '价格相对较贵', }, }, 'wahaha' : { '15' : { '总部位于中 ...

  5. head first python菜鸟学习笔记(第七章) ——web应用之为数据建模

    问题1. #意思是从athletelist.py中导入AthleteListfrom athletelist import AthleteList 源程序代码 import pickle from a ...

  6. head first python菜鸟学习笔记(第四章)

    1,p124,错误:NameError: name 'print_lol' is not defined 要想文件内如图显示,需要把调用BIF print()改为调用第二章的nester模块中的pri ...

  7. 【Android菜鸟学习之路】环境搭建问题-修改AVD Path

    更改avd默认路径

  8. Python学习--------------Atm+购物车系统

    一.程序需求 模拟实现一个ATM + 购物商城程序: 1.额度 15000或自定义 2.实现购物商城,买东西加入 购物车,调用信用卡接口结账 3.可以提现,手续费5% 4.每月22号出账单,每月10号 ...

  9. python学习:购物车程序

    购物车程序 product_list = [ ('mac',9000), ('kindle',800), ('tesla',900000), ('python book',105), ('bike', ...

  10. python入门学习:6.用户输入和while循环

    python入门学习:6.用户输入和while循环 关键点:输入.while循环 6.1 函数input()工作原理6.2 while循环简介6.3 使用while循环处理字典和列表 6.1 函数in ...

随机推荐

  1. appcrawler(2.1.3)采坑

    转发:https://www.jianshu.com/p/d97290136bad 遇到的坑: 问题1: Exception in thread "main" org.openqa ...

  2. Esp8266 Arduino PubSubClient连接阿里云老是返回rc=2错误的解决方案

    最近在用Esp8266 Arduino连接阿里云IOT的时候,服务器端老是返回rc=2 的错误! 用了MQTTfx模拟连接阿里云IOT没有问题,但是把同样的ClientID,UserName和Pass ...

  3. HIVE 调优思路和实践

    1,数据存储调优 1.1 设置压缩: 设置中间数据/输出结果压缩传输,使用snappy格式. hive-site.xml: set hive.exec.compress.output = true # ...

  4. 2023-03-01 react-native 实现 复制功能 @react-native-community/clipboard 报错:TypeError: null is not an object (evaluating 'NativeClipboard_1.default.setString')。

    我的react-native(下称rn)版本为0.68,要实现这个功能主要用到rn的clipboard,在21年的时候他就已经提示clipboard会在未来的版本中上去掉,官方的建议是不要再从reac ...

  5. 20_webpack_shimming预支全局变量和css的抽离

    shimming是什么 shimming 是一个概念,是某一些功能的统称 shimming(垫片),给我们的代码填充一些垫片来处理一些问题 比如我们现在以来一个第三方的库,这个第三方的库本身依赖lod ...

  6. BundleFusion_Ubuntu_Pangolin 安装的一些error

    /usr/bin/ld: 找不到 -lEigen3::Eigen 解决方法:find_package(Eigen3 REQUIRED)为list(APPEND CMAKE_INCLUDE_PATH & ...

  7. Python扩展(pybind11混编)

    背景介绍pybind11是一个基于C++11标准的模版库. 与Boost.Python类似, pybind11主要着眼于创建C++代码的Python封装, 并为其提供了一套轻量级的解决方案. 安装与代 ...

  8. Linux-samba共享

    CentOS8 中 Samba环境搭建(满足Linux和Windows系统之间文件共享传输) Samba服务器通信协议(Server Messages Block)就是是为了解决局域网内的文件或打印机 ...

  9. Hyperf微服务

    https://hyperf.wiki 状态码含义 https://segmentfault.com/a/1190000002523655

  10. YOLOV4网络

    Yolov4网络代码 from collections import OrderedDict import torch import torch.nn as nn from Darknet_53 im ...