• 【python】——购物车


    作业需求:

    用户名和密码存放于文件中,格式为:egon|egon123
    启动程序后,先登录,登录成功则让用户输入工资,然后打印商品列表,失败则重新登录,超过三次则退出程序
    允许用户根据商品编号购买商品
    用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
    可随时退出,退出时,打印已购买商品和余额



    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # Author:__Json.Zzgx__
    
    product_list = [
        ('iphone',5800),
        ('lenovo',30000),
        ('bike',800),
        ('tesla',20000),
        ('house',40),
        ('horse',10),
    ]
    
    name = 'egon'
    passwd = 'egon123'
    
    salary = 0
    shopping_list = []
    
    flag=False
    while True:
        if flag:
            break
        user = input("用户名: ").strip()
        pas = input("密码: ").strip()
        if  user == name and   pas == passwd:
            flag=False
            while True:
                salary = input("请输入您的工资:").strip()
                if salary.isdigit():
                    salary = int(salary)
                    flag=True
                    break
                print("请以数字的形式输入您的工资,请重新输入:")
                flag=False
        else:
            print('用户名或密码不正确')
            flag=False
            continue
    
    while True:
    
        print("
    ---------------- 商品列表 ----------------")
        can = False
        for index,buy in enumerate(product_list):
            if buy[1] <= salary:
                can = True
                print(index,"商品:%s 价格:%s元.按q退出购买" % (buy[0].center(8,' '), buy[1]))
        if not can:
            print("没有可以购买的商品,按q退出购买")
    
        user_choice = input("选择要买的商品编号 >>>: ").strip()
        if user_choice.isdigit():
            user_choice = int(user_choice)
    
            if user_choice < len(product_list) and user_choice >=0:
                p_item = product_list[user_choice]
    
                if p_item[1] <= salary: #买的起
                    shopping_list.append(p_item)
                    salary -= p_item[1]
                    print("
    ================================================")
                    print("商品:%s ,价格:%s元 已加入您的购物车" %(p_item[0],p_item[1]))
                    print("================================================
    ")
                    print("您的余额还剩 33[31;1m %s 33[0m 给您推荐如下商品:"  %(salary))
    
            else:
                print("商品[%s]不存在!" % user_choice)
    
        elif user_choice == 'q':
            print("
    ---------------- 购买列表 ----------------")
    
            listset = set(shopping_list)
    
            sum = 0
            for item in listset:
                print("商品:%s ,价格:%s元, 数量:%s 件" % (item[0], item[1],shopping_list.count(item)))
                sum += item[1] * shopping_list.count(item)
            print("------------------------------------------")
            print("
    您的余额还剩:",salary,"")
            print("总计消费:%s 元" %(sum))
            exit()
    
        else:
            print("您输入的商品编号有误,请重新输入")
    View Code

    【开源是一种精神,分享是一种美德】

      — By GoodCook

      — 笔者QQ:253097001

      — 欢迎大家随时来交流

      —原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。

     
  • 相关阅读:
    批量清理java源码的target目录
    前端移动node_modules到其他位置
    oracle物化视图和视图的数据不一致
    百词斩英语单词素材提取、听力练习
    2048自动游戏AI, 最高可以玩出一二十个2048
    switcheroo: Alt+Tab的替代工具、窗口搜索
    为知笔记wiz.editor.md增强
    腾讯北极星 Polaris 试用
    [分布式] 分布式事务、seata
    Mysql查询所有的表名和查询表中所有的字段名
  • 原文地址:https://www.cnblogs.com/goodcook/p/7520279.html
Copyright © 2020-2023  润新知