python文件部分:
user = {'username':None}
shopping_trolley = {}
shopping_num = [0]
money = [0]
my_money = [0]
#获取商品列表
def get_goods_list():
with open('shopping.txt', encoding='utf-8') as fr:
shopping_dic = eval(fr.read())
return shopping_dic
shopping_dic = get_goods_list()
#登录
def login():
if not user['username']:
print("欢迎来到登录页面^-^\n")
usename = input("请输入你的用户名:")
password = input("请输入你的密码:")
#打开文件判断文件中是否存在用户名和密码
with open('user_info.txt',encoding='utf-8') as fr:
data = fr.read().split('|')
user_info = (f"{usename}:{password}")
if user_info in data:
print("登录成功!")
print(f"欢迎{usename},你当前的账户余额为:{my_money[0]}\n")
user['username'] = usename
else:
print("登录失败")
else:
print(f"尊敬的:{user['username']},你已经登录,无需重复登录^-^")
#注册
def register():
print("欢迎来到注册页面^-^\n")
#获取用户注册的用户名和密码
username = input("请输入你的用户名:")
password = input("请输入你的密码:")
#将注册信以追加的形式存入文件
with open('user_info.txt','a') as fa:
fa.write(f"{username}:{password}|")
print("注册成功!")
#购物
def shopping():
if user['username']:
print("欢迎来到购物界面!^-^")
while True:
for k,v in enumerate(shopping_dic):
print(f"序号:{k},{v}")
get_goods_num = input("请输入商品序号(不需要则按q退出):")
if get_goods_num == 'Q' or get_goods_num =='q':
print(f"你当前购物车中有{shopping_num[0]}件物品")
break
if get_goods_num =="":
print("请输入正确的序号\n")
continue
get_goods_num = int(get_goods_num)
goods = shopping_dic[get_goods_num]
print(f"{goods}加入购物车成功\n")
goods_name = goods[0]
if goods_name in shopping_trolley:
shopping_trolley[goods_name] += 1
shopping_num[0] +=1
else:
shopping_trolley[goods_name] = 1
shopping_num[0] += 1
money[0] += goods[1]
else:
print("您当前尚未登录!")
#购物车
def shopping_list():
if user['username']:
print("欢迎来到购物车功能^-^")
while True:
print(f"你当前加入购物车的商品为{shopping_trolley},总价格为{money[0]}")
delete = input("请问是否移除商品,不移除则按q退出:")
if delete == 'Q' or delete =='q':
break
elif delete =="":
print("请输入正确的名称或字母\n")
continue
shopping_trolley[delete] -=1
if shopping_trolley[delete] == 0:
shopping_trolley.pop(delete)
for goods in shopping_dic:
if delete in goods:
money[0] -= goods[1]
shopping_num[0] -= 1
else:
print("请登录后再试!")
#结算
def pay():
if user['username']:
print("欢迎来到结算页面^-^")
print(f"您当前的账户余额为:{my_money[0]}")
while True:
print(f"您当前中的购物车中有:{shopping_trolley},总价为{money[0]}")
settle_account = input("是否结算,结算请输入y,退出请输入n(退出则会清空购物车):").lower()
if settle_account == 'y':
if my_money[0] > money[0]:
print(f"支付{money[0]}成功,已成功购买{shopping_trolley}")
shopping_num[0] = 0
my_money[0] = my_money[0] - money[0]
print(f"你当前余额为:{my_money[0]}元")
shopping_trolley.clear()
money[0] = 0
break
else:
print(f"你的余额不允许你这么做哦,你当前余额为{my_money[0]}←_←,请充值后再重试")
elif settle_account == 'n':
shopping_trolley.clear()
money[0] = 0
print("清空购物车成功!")
break
else:
print("输入不合法")
else:
print("请登录后再试!")
#充值
def recharge():
if user['username']:
print("欢迎来到充值界面^-^")
print(f"您当前余额为:{my_money[0]}")
while True:
top_up = input("请输入你充值的金额:")
if top_up.isdigit():
my_money[0] += int(top_up)
if my_money[0] != 0:
print("充值成功!")
print(f"您当前的账户余额为:{my_money[0]}")
break
elif top_up == "":
print("请输如你需要的数目!")
else:
print("充值失败")
continue
else:
print("请登录后再试!")
#退出登录
def exit():
if user['username']:
while True:
exi_login = input(f"尊敬的:{user['username']},是否退出登录,退出请输入y,取消请输入n:").lower()
if exi_login == 'y':
user['username'] = ""
print("退出成功!")
break
elif exi_login == 'n':
print("取消退出登录成功!")
break
else:
print("请输入y/n")
else:
print("你当前尚未登录!")
# 主函数
def mian():
mian_list = """
1:注册
2:登录
3:购物
4:购物车
5:结账
6:充值入口
7:退出登录
"""
mian_func = {
'1': register,
'2': login,
'3': shopping,
'4': shopping_list,
'5': pay,
'6': recharge,
'7': exit
}
mian_len = len(mian_func)
while True:
print(mian_list)
select_serv=input("请选择服务(按Q退出):")
if select_serv == 'Q' or select_serv =='q':
print("欢迎下次光临^-^")
break
if not select_serv.isdigit():
print('请输入数字!')
continue
# select_serv = int(select_serv)
if select_serv > str(mian_len):
print("你输入的数字不合法!")
continue
mian_func[select_serv]()
mian()
购物列表文件
[
['变形金刚', 199],
['玩具汽车', 299],
['电动玩具', 399],
['流光星陨', 499],
['阿波菲斯', 599],
['荒古遗尘', 699],
['圣耀救赎', 799],
['苍穹幕落', 899],
['夜雨黑瞳', 999],
['界·夜雨黑瞳', 1999]]