#good文件夹内容
[
["Iphone7", 5800],
["Coffee", 30],
["疙瘩汤", 10],
["Python Book", 99],
["Bike", 199],
["ViVo X9", 2499],
]
user = {"username": None}
shopping_car_dict = dict()
money = [0]
def get_goods():
with open("goods.txt","r",encoding="utf8") as fr:
goods_list = fr.read()
goods_list = eval(goods_list)
return goods_list
goods_list = get_goods()
def register():
print("请注册")
if not user["username"]:
username = input("username:")
pwd = input("pwd:")
with open("user_info.txt","a",encoding="utf8") as fa:
fa.write(f"{username}:{pwd} |")
else:
print("已登录,无法再注册")
def login():
print("欢迎登录")
if not user["username"]:
username = input("username:")
pwd = input("pwd:")
inp_user_info = f"{username}:{pwd}"
with open("user_info.txt","r",encoding="utf8") as fr:
user_info = fr.read()
user_info_split = user_info.split("|")
if inp_user_info in user_info_split:
print("登陆成功")
user["username"] = username
else:
print("账号或密码输入错误")
else:
print("您已登陆")
def shopping():
print("欢迎来到购物商场")
while True:
for ind, goods in enumerate(goods_list):
print(f"商品编号:{ind}",goods)
goods_choice = input("请输入您需要的商品,(输入q退出):")
if goods_choice == "q":
break
goods_choice = int(goods_choice)
goods = goods_list[goods_choice]
print(f"购物成功:{goods} ")
goods_name = goods[0]
if goods_name in shopping_car_dict:
shopping_car_dict[goods_name] += 1
else:
shopping_car_dict[goods_name] = 1
money[0] += goods[1]
print(f"购物车商品:{shopping_car_dict}, 总价:{money[0]}")
def shopping_car():
print("购物车")
while True:
print(f"购物车商品:{shopping_car_dict}, 总价:{money[0]}")
goods_choice = input("请输入不需要的商品,若没有请按q退出")
if goods_choice == "q":
break
shopping_car_dict[goods_choice] -= 1
for goods in goods_list:
if goods_choice in goods:
money[0] -= goods[1]
def pay():
print("请支付")
print(f"购物车商品:{shopping_car_dict}, 总价:{money[0]}")
choice = input("是否支付(Y/y),若不支付(N/n),不支付会清空购物车:")
if choice == "Y" or choice == "y":
print(f"支付{money[0]}成功,已购买{shopping_car_dict}")
elif choice == "N" or choice == "n":
shopping_car_dict.clear()
money[0] = 0
print("未支付,清空购物车成功")
else:
print("输入不合法")
func_msg = """
1:注册
2:登录
3:购物
4:购物车
5:结账
q:结束
"""
func_dict = {
"1": register,
"2": login,
"3": shopping,
"4": shopping_car,
"5": pay,
}
while True:
print(func_msg)
func_choice = input("请选择您需要的功能")
if func_choice == "q":
break
func_dict[func_choice]()