六月十二号作业

一:今日作业:
1、编写文件copy工具
x = input('请输入需要拷贝文件的路径')
with open(r'%s'%x,mode='rb') as f1 ,\
open(r'%s-new'%x,mode='wb') as f2:
z = f1.readlines()
f2.writelines(z)


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

while True:
msg = """
0 退出
1 登录
2 注册
"""
print(msg)
cmd = input('请输入命令编号>>: ').strip()
if not cmd.isdigit():
print('必须输入命令编号的数字,傻叉')
continue
if cmd == '0':
break

elif cmd == '1': #登录

q = 0
while q == 0:
iuser = input('请输入账号:').strip()
with open('8_2',mode='rt',encoding='utf-8') as f4:
x = f4.readlines()
y = 0
# print(x)
for xu in x:
xu = xu.strip()
if xu == iuser:
y += 1
# print(y)
if y > 3:
print('您已被锁定!')
break
else:
ipwd = input('请输入密码:').strip()
with open('8_1',mode='rt',encoding='utf-8') as f5:
for o1 in f5:
ous , ow = o1.strip("\n").split(':')
if iuser == ous and ipwd == ow:
print('登录成功')
q = 1
break

else:
print('登录失败')
with open('8_2',mode='at',encoding='utf-8') as f6:
f6.write(iuser)
f6.write('\n')
f6.flush()
gwc = []
while q == 1:
with open('8_3',mode='rt',encoding='utf_8') as f7:
sp = {}
for j in f7:
sm , sj = j.strip("\n").split(':')
sp[sm] = sj
print(' %s:%s'%(sm,sj))
print('0 退出')
print('查看购物车请按+')
p = input('请输入商品名')
if p == '0':
q = 2
elif p == '+':
if gwc == []:
print('购物车是空的')
else:
for gwcl in gwc:
print(r'购物车里有%s,共%s个,%s元'%(gwcl[0],gwcl[1],gwcl[2]))
input('任意键返回')

elif p not in sp:
print('商品不存在')
else:
while True:
print('%s每个%s元'%(p,sp[p]))
print('(取消请按0)')
g = input('购买个数:').strip()
if not g.isdigit():
print('必须输入命令编号的数字,傻叉')
elif g != '0':
jg = int(sp[p]) * int(g)
print('%s购买%s个,共花费%s元'%(p,g,jg))
gwc.append([p,g,jg])
break
else:
break


elif cmd == '2': #注册
a = 0
while a == 0:
user = input('请输入账号:').strip()
with open('8_1',mode='rt',encoding='utf-8') as f1:
ou = []
for o in f1:
ouser , opwd = o.strip("\n").split(':')
ou.append(ouser)
if user in ouser:
print('用户名已存在')
continue
else:
while True:
pwd1 = input('请输入密码:').strip()
pwd2 = input('请确认密码:').strip()
if pwd1 == pwd2:
with open('8_1',mode='at',encoding='utf-8') as f3:
f3.write('%s:%s'%(user,pwd1))
print('注册成功')
a = 1
break
else:
print('密码输入有误')


else:
print('输入的命令不存在')
posted @ 2020-06-12 01:40  最冷不过冬夜  阅读(58)  评论(0编辑  收藏  举报