函数基础实战之ATM和购物车系统
一、系统功能
- 用户名和密码存放于文件中,格式为:randy|123456
- 启动程序后,先登录,登录成功则让用户输入工资,然后打印商品列表,失败则重新登录,超过三次则退出程序
- 允许用户根据商品编号购买商品
- 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
- 可随时退出,退出时,打印已购买商品和余额
[
['Iphone7', 5800],
['Coffee', 30],
['疙瘩汤', 10],
['Python Book', 99],
['Bike', 199],
['ViVo X9', 2499],
]
二、逻辑代码实现
user_dic = {'username': None}
user_goods_dic = dict()
money = [0]
def login():
"""登录模块"""
print("欢迎来到登录界面!")
user_name = input("请输入用户名:").strip()
user_pwd = input("请输入密码:").strip()
user_info = f'{user_name}:{user_pwd}'
# 实现登录验证
with open('user_info', 'r', encoding='utf-8') as fr:
data = fr.read()
data_list = data.split("
")
if user_info in data_list:
print("登录成功!你可以去购物啦!")
user_dic["username"] = user_name
else:
print("账号或密码错误!")
def register():
"""注册模块"""
if not user_dic['username']:
print("欢迎来到注册页面!")
user_name = input("请输入用户名:").strip()
user_pwd = input("请输入密码:").strip()
# 实现注册
with open('user_info', 'a', encoding='utf-8') as fa:
fa.write(f'{user_name}:{user_pwd}
')
print('注册成功!')
else:
print("你已经登录了!")
def get_goods():
"""获取商品"""
with open('shopping', 'r', encoding='utf-8') as fr:
googs = fr.read()
googs_list = eval(googs)
return googs_list
def shopping():
"""购物模块"""
if user_dic['username']:
print("欢迎来到购物界面!")
global user_goods_dic
googs_list = get_goods()
while True:
for i, em in enumerate(googs_list):
print(f"商品编号:{i} {em}")
googs_choice = input("请输入商品编号(输入q退出):").strip()
if googs_choice == 'q':
break
googs_choice = int(googs_choice)
# 通过列表索引保存数据
googs_res = googs_list[googs_choice]
googs_name = googs_res[0]
googs_price = googs_res[1]
# 将用户商品加入购物车
if googs_name in user_goods_dic:
user_goods_dic[googs_name] += 1
else:
user_goods_dic[googs_name] = 1
print(f'你选择的商品{googs_name}加入购物车')
money[0] += googs_price
print(f"购物车商品为{user_goods_dic} 共花费了{money[0]}元")
else:
print("你没有登录!请先登录!")
def shopping_car():
"""购物车模块"""
if len(user_goods_dic) > 0:
print("欢迎来到购物车界面!")
# 是否删除购物车
while True:
print(f"购物车商品为{user_goods_dic} 共花费了{money[0]}元")
shoop_choice = input("请问需要删除哪个商品并输入商品名,不需要删除请按q退出: ")
if shoop_choice == 'q':
break
user_goods_dic[shoop_choice] -= 1
googs_list = get_goods()
# 减掉价格
for googs in googs_list:
money[0] -= googs[1]
else:
print("购物车为空,快去购买!")
def pay():
"""支付模块"""
if money[0] != 0:
print("欢迎来到支付界面")
print(f"购物车商品为{user_goods_dic} 共花费了{money[0]}元")
confirm_choice = input("是否需要支付(Y/y),不支付会清空购物车(N/n):").lower()
# 判断是否购买
if confirm_choice == 'y':
print(f'购买商品为{user_goods_dic} 共花费了{money[0]}元')
print("正在准备发货,请耐心等待!")
elif confirm_choice == 'n':
user_goods_dic.clear()
money[0] = 0
print('取消成功!祝你购物愉快!')
else:
print('输入不合法!')
else:
print('你还没有购物呢!')
func_msc = """
[1] 登录
[2] 注册
[3] 购物
[4] 购物车
[5] 支付
[q] 退出
"""
func_dict = {
"1": login,
"2": register,
"3": shopping,
"4": shopping_car,
"5": pay
}
def func_mian():
while True:
print(func_msc)
func_choice = input("请选择功能(输入q退出):").strip()
if not func_choice.isdigit() or len(func_choice)>1:
print("请输入正确数值!")
continue
# 退出主程序
if func_choice == 'q':
break
func_dict[func_choice]()
if __name__ == "__main__":
# 主程序
func_mian()