需求及代码
# -*- encoding: utf-8 -*-
"""需求:
01:双色球(假设一共八个球,6个线球,球号1-32,2个蓝球,球号1-16)
02:确保用户不能重复选择,不能超出范围
03:用户输入有误时有相应的错误提示
04:最后展示用户选择的双色球的号码
"""
# Define the range of red/blue balls(定义红/蓝球的范围)
red_ball_range = list(range(1,33))
blue_ball_range = list(range(1,17))
# Prompt user to select(提示用户选择)
red_ball_tips = "\033[0;31m{}select red ball:\033[0m"
blue_ball_tips = "\033[0;34m{}select blue ball:\033[0m"
# Out-of-scope and repetitive tips(超出范围和重复提示)
red_out_range = "only can select n betwend 1-32"
blue_out_rang = "only can select n betwend 1-16"
red_ball_repeat = "number {} is already exist in red ball list"
blue_ball_repeat = "number {} is already exists in blue ball list"
# User Red/Blueball Selection Results(用户红/蓝球选择结果)
red_ball_result = []
blue_ball_result = []
# while loop
count = 1
while count < 9:
if count <= 6:
# 提示用户选择红球
red_ball_choice = input(red_ball_tips.format({count}))
# 判断用户输入的是不是整数(不是python认为的整数哈)
if len(red_ball_choice.strip()) == 0 or not red_ball_choice.isdigit():
print(red_out_range)
continue
# 将用户输入的整数转为python认为的整数
red_ball_choice_int = int(red_ball_choice)
# 判断转换成int后的值是否在红球的范围内
if red_ball_choice_int > 32 or red_ball_choice_int < 1:
print(red_out_range)
continue
# 判断用户当前选择的红球与之前选择的红球是否重复
# 若不重复则保存,则存在,则给了相应的提示
if red_ball_choice_int in red_ball_result:
print(red_ball_repeat.format(red_ball_choice_int))
continue
else:
red_ball_result.append(red_ball_choice_int)
count += 1
elif count > 6:
# 提示用户选择蓝球
blue_ball_choice = input(blue_ball_tips.format({count}))
# 判断用户输入的是不是整数 (不是python认为的整数哈)
if len(blue_ball_choice.strip()) == 0 or not blue_ball_choice.isdigit() :
print(blue_out_rang)
continue
# 将用户输入的整数转炎python认为的整数
blue_ball_choice_int = int(blue_ball_choice)
# 判断用户输入的是不是整数(不是python认为的整数哈)
if blue_ball_choice_int > 16 or blue_ball_choice_int < 1:
print(blue_out_rang)
continue
# 判断用户当前选择的红球与之前选择的红球是否重复
# 若不重复则保存,则存在,则给了相应的提示
if blue_ball_choice_int in blue_ball_result:
print(blue_ball_repeat.format(blue_ball_choice_int))
continue
else:
blue_ball_result.append(blue_ball_choice_int)
count += 1
# 打印用户选择的红球和蓝球
print(red_ball_result)
print(blue_ball_result)
代码执行结果