努力努力再努Li

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
复制代码
# coding: utf-8

job_rule_detail = '''
作业:双色球选购
1、双色球(假设一共八个球,6个红球,球号1-32、2个蓝球,球号1-16)
2、确保用户不能重复选择,不能超出范围
3、用户输入有误时,有相应的错误提示
4、最后展示用户选择的双色球的号码
升级需求:
1、一个while循环
'''
print(job_rule_detail)

"""
双色球作业-知识点整理:
1、首行编码,推荐使用: # coding: utf-8
2、接收用户输入的内容,不可直接转为int,易出错。建议通过isdigit()判断后再转换。
         【注意:-1 或 -6 等负数写法,会被isdigit()判断为非数字,因为“-”不是一个digit。】
3、字符串拼接官方推荐: str.format()。
4、使用标识符来改变判断条件: state = 1 or 0 ,或者 使用布尔值。
5、不推荐 while  else 写法。
6、设置打印文字的颜色。【参考:https://www.cnblogs.com/yyyyyyyyyy/p/9048338.html】

思路:
1、首先,定义变量,用于存储用户的输入
2、然后,编写while循环 以及 选择双色球逻辑
3、最后,打印用户的选择双色球
用到的知识点:列表操作、len()获取列表长度等
"""

# 欢迎标语
print("Welcome to 小猿圈 lottery station")
# 用户选择的红球及数量
user_red_ball = []
# 用户选择的篮球及数量
user_blue_ball = []
# 双色球选择完成状态,默认为False
selected_complete_status = False

# 当双色球选择完成状态为False时,进入while循环。
while not selected_complete_status:
    # 当用户已选择的红球数量等于6,并且篮球数量等于2,则设置双色球选择完成状态为True。
    if len(user_red_ball) == 6 and len(user_blue_ball) == 2:
        # 展示用户选择的双色球的号码
        print("\nRed ball:" + str(user_red_ball))
        print("Blue ball:" + str(user_blue_ball))
        print("Good Luck.")
        selected_complete_status = True
        continue
    elif len(user_red_ball) < 6:
        # 依次选择6个红球
        user_select_number = input("\033[1;31m[{0}]select red ball:\033[0m".format(str(len(user_red_ball) + 1)))
        # 如果用户输入为数字,则进行int转换。如果用户输入非数字, 则提醒用户并重新输入。
        if user_select_number.isdigit():
            user_select_number = int(user_select_number)
        else:
            print("only can select in between 1-32")
            continue
        # 红球球号必须在1-32
        if user_select_number < 1 or user_select_number > 32:
            print("only can select in between 1-32")
            continue
        # 不能重复选择
        elif user_select_number in user_red_ball:
            print("number {0} is already exist in red ball list".format(user_select_number))
            continue
        else:
            user_red_ball.append(user_select_number)
            continue
    elif len(user_blue_ball) < 2:
        # 依次选择2个蓝球
        user_select_number = input("\033[1;34m[{0}]select blue ball:\033[0m".format(str(len(user_blue_ball) + 1)))
        # 如果用户输入为数字,则进行int转换。如果用户输入非数字, 则提醒用户并重新输入。
        if user_select_number.isdigit():
            user_select_number = int(user_select_number)
        else:
            print("only can select in between 1-32")
            continue
        # 蓝球球号必须在1-16
        if user_select_number < 1 or user_select_number > 16:
            print("only can select in between 1-16")
            continue
        # 不能重复选择
        elif user_select_number in user_blue_ball:
            print("number {0} is already exist in blue ball list".format(user_select_number))
            continue
        else:
            user_blue_ball.append(user_select_number)
            continue
复制代码
# 打印效果截图如下:

 

posted on   努力努力再努Liz  阅读(295)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示