python作业四,,作业题目:购物车程序


作业题目:购物车程序

作业需求:

数据结构:goods = [{"name": "电脑", "price": 1999},{"name": "鼠标", "price": 10},{"name": "游艇", "price": 20},{"name": "美女", "price": 998},......]功能要求:基础要求:1、启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表2、允许用户根据商品编号购买商品3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒4、可随时退出,退出时,打印已购买商品和余额5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示扩展需求:1、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买2、允许查询之前的消费记录

基础要求代码:

goods = [{"name": "电脑", "price": 1999},{"name": "鼠标", "price": 10},{"name": "游艇", "price": 20},{"name": "美女", "price": 998},]shopping =[]username = [‘alex‘]passname = [‘123‘]Uname = input("用户名:")Pname = input("密码:")if username[0]==Uname and passname[0]==Pname:    print("登录成功!")    salary=input("输入工资:")    if salary.isdigit():        salary=int(salary)        while True:            for index ,item in enumerate(goods):                 print(index,item)            user_choice=input("选择要买什么?>>>:")            if user_choice.isdigit():                user_choice = int(user_choice)                if user_choice < len(goods) and user_choice>=0:                    p_item = goods[user_choice]                    if p_item["price"] <= salary:                        shopping.append(p_item)                        salary -= p_item["price"]                        print("还剩\033[31;1m%s\033[0m"%(salary))                    else:                        print("余额不足")                else:                    print("商品不存在")            elif user_choice == ‘q‘:                print("----------shopping list----------")                for p in shopping:                    print(p)                print("余额还剩\033[31;1m%s\033[0m"%(salary))                exit()            else:                print("无效选项")else:    print("登录失败!")

  

python作业四

评论关闭