一个炒鸡简单的购物车
# 我自己新建了两个文件;一个存放用户的user_info.txt文件和存放商品信息的goods_info
choose_goods_dict = {}
user_lt = []
def register():
"""注册"""
print('*'*10,'欢迎来到注册功能!','*'*10)
username_inp = input("请设置你的用户名:")
pwd_inp = input("请输入你的密码:")
register_count = 0
while register_count <3:
re_pwd_inp = input("请确认你的密码:")
if pwd_inp != re_pwd_inp:
print('两次输入的密码不同,请重新输入')
register_count +=1
continue
with open(r'user_info.txt','a',encoding='utf-8') as fa:
fa.write(f'{username_inp}:{pwd_inp}\n')
print('恭喜注册成功!')
user_lt.append(username_inp)
return
def login():
""""登录"""
if not user_lt == []:
print('您已登录过了,请先注销再重新登录')
return
print('*' * 10, '欢迎来到登录功能!', '*' * 10)
login_count = 0
while login_count < 3:
username_inp = input('请输入你的用户名:')
pwd_inp = input('请输入你的密码:')
with open(r'user_info.txt', 'r', encoding='utf8') as fr:
for i in fr:
user_info = i.strip()
username, pwd = user_info.split(':')
if username_inp == username and pwd_inp == pwd:
print('登录成功!')
user_lt.append(username)
print(user_lt)
return
else:
print('登录失败')
login_count += 1
def shopping():
"""购物"""
if user_lt == []:
print('请先登录!')
return
print('*' * 10, '欢迎来到购物功能!', '*' * 10)
with open(r'goods_info.txt','r',encoding='utf-8') as fr:
goods_info = fr.read()
print(f'商品如下:\n{goods_info}')
while True:
goods_inp = input('请输入你想要的商品,按z结束购物')
goods_dict = eval(goods_info)
if goods_inp not in goods_dict:
print('请选择正确的数字!')
continue
goods = goods_dict[goods_inp]
if goods in choose_goods_dict:
choose_goods_dict[goods] +=1
elif goods not in choose_goods_dict:
choose_goods_dict[goods] = 1
print(f'{goods}已成功加入购物车')
if goods_inp == 'z':
print(f'您总共购买了:{choose_goods_dict},请前往购物车结算!')
return
def shopping_cart():
"""购物车结算"""
if user_lt == []:
print('请先登录!')
return
print('*' * 10, '欢迎来到购物车!', '*' * 10)
print(f'您购物车里有{choose_goods_dict},是否结算?')
buy_count = 0
while buy_count<3:
buy_inp = input('请输入Y/N:')
if buy_inp != 'Y' and buy_inp != 'N':
print('请重新输入')
buy_count +=1
continue
if buy_inp == 'Y':
print('结算成功!')
return
if buy_inp =='N':
print('结算失败!请继续操作')
return
def logout():
"""注销"""
if user_lt == []:
print('请先登录!')
return
else:
user_lt.clear()
print('注销成功!')
return
func_dict = {
'1': register,
'2': login,
'3': shopping,
'4': shopping_cart,
'5': logout
}
while True:
print('*' * 50,
"""
1 注册
2 登录
3 购物
4 购物车结算
5 注销
q 退出\n""", '*' * 50)
choose_inp = input('请选择功能:')
if choose_inp == 'q':
print('欢迎下次再来')
break
if choose_inp not in func_dict:
print('请选择我们有的功能!')
continue
func_dict[choose_inp]()