周末作业。二

  写一个购物车,可以注册。登录。购物。支付。充值:

import os
def register():
tag = True
while tag:
name = input('请输入用户名>>:').strip()
pwe = input('请输入密码>>:').strip()
with open('ab.txt',mode='a+t',encoding='utf-8') as f:
f.seek(0)
for line in f:
a = line.strip('\n').split(':')
if name in a:
print('用户名已存在')
break
else:
info = '%s:%s\n'%(name,pwe)
f.write(info)
print('注册成功')
tag = False
def login():
tag = True
dic = {}
while tag:
name = input('请输入用户名>>:').strip()
pwe = input('请输入密码>>:').strip()
with open('ab.txt',mode='rt',encoding='utf-8') as read_f,\
open('black.list.txt',mode='a+t',encoding='utf-8') as write_f:
write_f.seek(0)
for line in write_f:
u = line.strip('\n')
if u == name:
print('你已在黑名单内')
tag = True
break
else:
for line in read_f:
a = line.strip('\n').split(':')
if name == a[0] and pwe == a[1]:
print('登陆成功')
return name
else:
print('密码输入错误')
read_f.seek(0)
for line in read_f:
a = line.strip('\n').split(':')
if name in a:
if name not in dic:
dic[name]=1
else:
dic[name]+=1
if dic[name]==3:
write_f.write('%s\n'%(name))
tag = False
break


def shopping():
msg_dic={'alex':1,'egon':666,'iphoneX':12899,'mac':14899,}
good_l=[]
while True:
for key,item in msg_dic.items():
print('{name} price: {price}'.format(name=key,price=item))
choice = input('请输入商品名>>:').strip()
if not choice or choice not in msg_dic:
print('商品名输入有误,请重新输入')
continue
count =input('请输入购买个数>>:').strip()
if not count.isdigit():
print('输入有误,请重新输入')
continue
count = int(count)
good_l.extend([choice,msg_dic[choice],count,count*msg_dic[choice]])
print(good_l)
return good_l

def pay():
with open('ab.txt',mode='rt',encoding='utf-8')as read_f,\
open('cd.txt',mode='wt',encoding='utf-8') as write_f:
for line in read_f:
line = line.strip('\n').split(':')
if name == line[0]:
line[2]=int(line[2])
good_l[3]=int(good_l[3])
data=line[2]-good_l[3]
if data < 0:
print('账户余额不足,请充值')
return
print('购买成功,请继续选择')
data = str(data)
line[2] = str(line[2])
line = ':'.join(line).replace(line[2],data)
write_f.write('%s\n'%(line))
else:
line = ':'.join(line)
write_f.write('%s\n'%(line))
os.remove('ab.txt')
os.rename('cd.txt','ab.txt')

def rec():
with open('ab.txt',mode='rt',encoding='utf-8')as read_f,\
open('cd.txt',mode='wt',encoding='utf-8')as write_f:
for line in read_f:
a = line.strip('\n').split(':')
if name == a[0]:
b = input('请输入你要充值的金额>>:').strip()
if b.isdigit():
b = int(b)
if b<=0:
print('输入金额有误,请重新输入')
else:
print('充值成功')
a[2]=int(a[2])
data = a[2]+b
a[2]=str(a[2])
data=str(data)
a = ':'.join(a).replace(a[2],data)
write_f.write('%s\n'%(a))
else:
a = ':'.join(a)
write_f.write('%s\n'%(a))
os.remove('ab.txt')
os.rename('cd.txt','ab.txt')

tag = True
while tag:
print('''欢迎来到小吕通讯专卖店:
1.注册
2.登录
3.退出''')
choice = input('请选择你要用的功能>>:')
if choice == '1':
register()
elif choice == '2':
name=login()
while tag:
if name:
print('''请先选择购物再进行支付,要不都报错!
1.购物车
2.支付
3.充值
4.退出''')
choice = input('请选择>>:')
if choice == '1':
good_l= shopping()
elif choice == '2':
pay()
elif choice == '3':
rec()
elif choice == '4':
tag = False
elif choice == '3':
tag = False

猜你喜欢

转载自www.cnblogs.com/lvyipin1/p/9697468.html