shopping_dict = {
'1':'a',
'2':'b',
'3':'c',
'4':'d',
}
shopping_car = {}
name_list = []
def register():
if name_list:
print('已经登入,请注销后注册')
return
count = 0
while count < 3:
name_inp = input('输入名字:')
pwd_inp = input('输入密码:')
pwd_inp2 = input('再次输入密码:')
if not pwd_inp == pwd_inp2 :
print('俩次密码不一致')
count += 1
continue
with open(r'jsb.txt','a',encoding='utf-8')as fa:
fa.write(f'{name_inp}:{pwd_inp}\n')
fa.flush()
print('注册成功')
return
def login():
if name_list:
print('不要重复登入')
return
count = 0
while count < 3:
name_inp = input('输入名字:')
pwd_inp = input('输入密码:')
with open(r'jsb.txt','r',encoding='utf-8')as fr:
for jsb in fr:
jsb = jsb.strip()
name,pwd = jsb.split(':')
if name == name_inp and pwd == pwd_inp:
print(f'欢迎{name_inp}登入')
name_list.append(name_inp)
return
else:
print('帐号密码错误')
count += 1
def logout():
if not name_list:
print('登入后使用')
return
name_list.clear()
print('注销成功')
def gouwu():
if not name_list:
print('登入后使用')
return
print('''1 a
2 b
3 c
4 d''')
shopping_xuan = input('选择你需要的商品编号:')
shopping_name = shopping_dict[shopping_xuan]
if shopping_name in shopping_car:
shopping_car[shopping_name] += 1
else:
shopping_car[shopping_name] = 1
print(f'消费成功{shopping_name},所购商品有{shopping_car}')
def gouwuche():
if not name_list:
print('请登入后使用')
return
print(f'恭喜购物成功:{shopping_car}')
shopping_car.clear()
xuan_dict = {
'1' : register,
'2' : login,
'3' : logout,
'4' : gouwu,
'5' : gouwuche,
}
while True:
print('''1 注册
2 登入
3 注销
4 购物
5 购物车
0 退出''')
xuan_inp = input('选择功能:')
if xuan_inp == '0':
break
if xuan_inp not in xuan_dict:
print('输入数字')
continue
xuan_dictxuan_inp