day09作业

echo "陈少最帅!!!"

  输出结果 : 陈少最帅!!!


# 1、编写文件copy工具

# with open('../今日内容.txt', 'rb') as f:
# ret = f.read() # 读取文件数据
# with open('../今日内容_cp.txt', 'ab')as f:
# f.write(ret) # 复制到新文件文本中

"""
2、编写简单购物车程序,自己分析逻辑,完成编程:
1、先要求用户注册
2、注册完成后,可以登录
3、登录成功后,从文件中读取商品信息(名字、价钱)展示给用户
4、用户可以选择要购买的商品和购买的个数

"""

tag = True
while tag:
def decide(): # 总函数
tittle()
cmd = input('请输入数字')
if cmd == '1':
login()
elif cmd == '2':
register()
elif cmd.upper() == 'Q':
pass
else:
print('输入有误,重新输入')

def register(): # 注册
name = input('请输入注册账号:').strip()
pasd = input('请输入注册密码:').strip()
with open('login.txt','at',encoding='utf-8') as f:
f.write('%s:%s '%(name,pasd))

def login(): # 登录
name = input('请输入账号>>').strip()
pasd = input('请输入密码>>').strip()
with open('login.txt','rt',encoding='utf-8') as f:
for line in f:
v, k = line.strip(' ').split(':')
if name == v and pasd == k:
goods()

else:
print('账号或密码有误!')

def tittle():
print('''
登录 : 1
注册 : 2
退出 : q
''')

def goods(): # 进入选商品页面
with open('goods.txt','rt',encoding='utf-8') as f:
for line in f:
name, price = line.strip(' ').split(':')
print('名称:%s 价格:%s'%(name,price))
print('登录成功! 欢迎进入商品选购页面 ^ ')
index()

def index(): # 把商品加入购物车, 打印购物车
dic = {} # 用户选定后 存放用户商品名和数量
goods1 = [] # 存放商品名 列表
dic1 = {} # 从文件中取出后存放商品和价格
with open('goods.txt', 'rt', encoding='utf-8') as f:
for line in f:
name1, price = line.strip(' ').split(':')
goods1.append(name1)
dic1[name1]=price
while True: # 判断判断
name = input('请输入商品名称! "q或Q结算"').strip()
if name in goods1: # 判断用户输入的商品在仓库中是否存在
print('名称:%s 价格:%s'%(name,dic1[name]))
count = input('请输入数量:').strip()
if count.isdigit():
if name in dic: # 判断是否已经加购
dic[name] += int(count) # 加了直接加数量
else:
dic[name] = int(count) # 加新
print(dic)
else:
print('个数请输入数字!')
continue
if name.upper() == 'Q': # 结算
pass
else:
continue

def total(): # 计算总价格
pass
decide()


原文地址:https://www.cnblogs.com/Knge/p/13096431.html