• 路飞学城系列:第1章 Python基础语法入门-作业(1)【双色球选购-中文-自由发挥】


    # coding: utf-8
    
    """
    作业:双色球选购
    1 双色球(假设一共八个球,6个红球,球号1-32、2个蓝球,球号1-16)
    2 确保用户不能重复选择,不能超出范围
    3 用户输入有误时有相应的错误提示
    4 最后展示用户选择的双色球的号码
    升级需求:
    1 一个while循环
    
    思路:
    # 定义变量,存放已选购的双色球
    # 选购红球
    # 选购蓝球
    # 选购完成,打印
    """
    
    # 打印欢迎语
    print("************** 欢迎进入 牛魔王 双色球商店 ********")
    
    # 已选购的红色球
    selected_red_ball = []
    # 已选购的蓝色球
    selected_blue_ball = []
    # 双色球选购完成状态,True为选购完成,False为选购未完成
    selected_status = False
    
    # 通过while循环 + if elif,完成双色球选购逻辑
    while not selected_status:
    
        # 已选购红球数量
        selected_red_ball_len = len(selected_red_ball)
        # 已选购蓝球数量
        selected_blue_ball_len = len(selected_blue_ball)
    
        # 选购红球
        if selected_red_ball_len < 6:
            # 接收用户选购的红球号码
            user_select_ball = input("33[1;31m请选择第[{0}]颗红色球:33[0m".format(selected_red_ball_len + 1))
            # 判断球号是否为数字,如果是数字,则将user_select_ball转换为int类型
            if user_select_ball.isdigit():
                user_select_ball = int(user_select_ball)
                # 判断红球球号是否在1-32之间
                if user_select_ball < 1 or user_select_ball > 32:
                    print("红球球号必须是1-32之间的数字")
                    continue
                # 球号不可重复选择
                elif user_select_ball in selected_red_ball:
                    print("{0}号球已经被选购过了".format(user_select_ball))
                    continue
                # 满足选购条件,把球号添加到已选购红球列表
                else:
                    selected_red_ball.append(user_select_ball)
                    continue
            else:
                print("红球球号必须是1-32之间的数字")
                continue
        # 选购蓝球
        elif selected_blue_ball_len < 2:
            # 接收用户选购的号码
            user_select_ball = input("33[1;34m请选择第[{0}]颗蓝色球:33[0m".format(selected_blue_ball_len + 1))
            # 判断球号是否为数字,如果是数字,则将user_select_ball转换为int类型
            if user_select_ball.isdigit():
                user_select_ball = int(user_select_ball)
                # 判断蓝球球号是否在1-16之间
                if user_select_ball < 1 or user_select_ball > 16:
                    print("蓝球球号必须是1-16之间的数字")
                    continue
                # 球号不可重复选择
                elif user_select_ball in selected_blue_ball:
                    print("{0}号球已经被选购过了".format(user_select_ball))
                    continue
                # 满足选购条件,把球号添加到已选购蓝球列表
                else:
                    selected_blue_ball.append(user_select_ball)
                    continue
            else:
                print("蓝球球号必须是1-16之间的数字")
                continue
        # 选购完成,打印
        # 当红色球已选购6个,并且蓝色球已选购2个时,更新selected_status为True,并且打印展示
        elif selected_red_ball_len == 6 and selected_blue_ball_len == 2:
            selected_status = True
            print("
    您已选购的红球:{0}".format(selected_red_ball))
            print("您已选购的蓝球:{0}".format(selected_blue_ball))
    
    # 打印结尾语
    print("************** 选购结束,谢谢光临! **************")
    # 代码执行效果截图如下:



  • 相关阅读:
    HTML基础知识笔记摘要
    Shiro安全框架的说明及配置入门
    空指针问题的解决
    Log4j的配置及使用
    有关于注解的说明
    SpringMVC整合mybatis基于纯注解配置
    使用springmvc进行文件的上传和下载
    数据库设计入门及ERMaster的安装和使用
    spring mvc 接收ajax 复杂结构数据
    idea git ignore 插件
  • 原文地址:https://www.cnblogs.com/lizhen416/p/13509652.html
Copyright © 2020-2023  润新知