Python学习_第一阶段_购物车
作业要求
goods = [ {"name":"电脑","price":"1999"}, {"name":"鼠标","price":"10"}, {"name":"游艇","price":"20"}, {"name":"美女","price":"998"} ] 功能需求: 1. 启动程序后, 输入用户名密码后, 让用户输入工资, 然后打印商品列表 2. 允许用户根据商品编号购买商品 3. 用户选择商品后, 检查余额是否够, 够直接扣款, 不够就提醒 4. 可随时退出, 退出时, 打印已购买商品和余额 5. 在用户使用过程中, 关键输出, 如余额 商品已加入购物车等信息 需要高亮显示 扩张需求: 1. 用户下一次登录后, 输入用户名密码, 直接回到上次的状态, 即上次消费的余额什么的还是 哪些, 再次登录可继续购买 2. 允许查询之前的消费记录
1. 完成作业的基本要求
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#encoding:utf-8 import sys goods = [ {"name":"电脑","price":"1999"}, {"name":"鼠标","price":"10"}, {"name":"游艇","price":"20"}, {"name":"美女","price":"998"} ] shop_car = [] user_passwd = 'user_passwd.txt' lock_user = 'lock_user.txt' # 注册 def register(): print("欢迎来到注册页面".center(50,'*')) user = input("请输入您的用户名: ") passwd = input("请输入您的密码: ") passwd1 = input("请再次输入您的密码: ") if passwd == passwd1: with open(user_passwd, 'a+') as f: f.write(user+'|'+passwd+'\n') print("账号注册成功".center(50,'*')) # 登录 def login(user,passwd): with open(user_passwd,'r+') as f: for i in f: username, password =i.strip().split('|') if username == user and password == passwd: print("用户 %s 登录成功".center(50,'*')%(user)) return 'login_success' # 检查用户 def check_user(user): with open(lock_user,'r') as f: for i in f: if i.strip() == user: print('%s 被锁定'%(user)) sys.exit() # 购物商场 def network_shop(): salary = int(input("请输入你的工资: ").strip()) while True: print("XXXX商场有如下商品欢迎换购".center(66, '*')) for index, shop_name in enumerate(goods): print(index, shop_name) number = input("请输入商品的编号或者退出(exit) ").strip().lower() if number.isdigit(): number = int(number) elif number =='exit': print('退出XXXX网上购物商场') if len(shop_car) > 0: print("剩余金额为:",salary) print("您购买的商品为: ") for i in shop_car: print(i) break else: print("\n输入有问题, 请重新选择\n") continue if int(goods[number]['price']) <= salary: shop_car.append(goods[number]) salary -= int(goods[number]['price']) if len(shop_car) > 0: print("您购买的商品为: ") for i in shop_car: print(i) elif int(goods[number]['price']) > salary: print("余额不足, 账户剩余金额为: ",salary) recharge_memory = input("是否充值: ").strip().lower() if recharge_memory == 'n': if len(shop_car) >0: print("剩余金额为:", salary) print("您购买的商品为: ") for i in shop_car: print(i) break elif recharge_memory == 'y': salary +=int(input("请输入你要充值的金额").strip()) # 登录的主逻辑 def login_main(): while True: print('欢迎来到XXXXX网上商城'.center(50,'*')) have_user = (input("是有账号,y|n: ").strip()).lower() if have_user == 'y': user = input('请输出账号: ').strip() check_user(user) passwd = input('请输出密码: ').strip() if login(user, passwd) == 'login_success': return "login_success" elif have_user == 'n': new_register = (input("是否注册,y|n: ").strip()).lower() if new_register == 'y': register() continue elif new_register == 'n': break else: print("输入有问题, 重新输入: ") # 启动程序 def main(): if login_main() == "login_success": network_shop() if __name__ == '__main__': main()
posted on 2019-03-01 17:59 augustyang 阅读(183) 评论(0) 编辑 收藏 举报