# 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
# 打印效果截图如下:
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步