作业需求
数据结构: goods = [ {"name": "电脑", "price": 1999}, {"name": "鼠标", "price": 10}, {"name": "游艇", "price": 20}, {"name": "美女", "price": 998}, ...... ] 功能要求: 基础要求: 1、启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表 2、允许用户根据商品编号购买商品 3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 4、可随时退出,退出时,打印已购买商品和余额 5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示 扩展需求: 1、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买 2、允许查询之前的消费记录
代码:
# -*- coding:utf-8 -*- #Author:Kris goods = [ {"name": "电脑", "price": 1999}, {"name": "鼠标", "price": 10}, {"name": "游艇", "price": 20}, {"name": "美女", "price": 998}, ] shopping_cart = [] _name = "kris" _passworld = "abc1234" name = input("请输入用户名:") passworld = input("请输入密码:") if name == _name and passworld ==_passworld: salary = int(input("请输入你的工资(元):")) else: print("您的用户名或密码错误") exit() while True: print("--------商品列表--------") for index,i in enumerate (goods): print("%s.%s %s" %(index,i["name"],i["price"])) choice = (input("请输入你要买的商品编号或输入q退出:")) if choice.isdigit(): choice = int(choice) if choice >=0 and choice <=len(goods): if (salary - int(goods[choice]["price"])) >= 0: shopping_cart.append(goods[choice]) salary -= goods[choice]["price"] print("Adding %s into the shopping cart" %(goods[choice]["name"])) print("还剩余%d元" %(salary) ) else: print("您的余额不足") else: print("此商品编号不存在") if len(shopping_cart) > 0: print("----------您已购买以下商品-----------") for index2,i2 in enumerate (shopping_cart): print(index2,i2) elif choice == 'q': exit()