一、需求
01:双色球(假设一共八个球,6个红球,球号1-32、2个蓝球,球号1-16)
02:确保用户不能重复选择,不能超出范围
03:用户输入有误时有相应的错误提示
04:最后展示用户选择的双色球的号码
二、代码
# -*- encoding: utf8 -*-
# 定义红球和蓝球的范围
READ_BALL = list(range(1,33))
BLUE_BALL = list(range(1,17))
# 选择红球和蓝球时的提示
READ_BALL_TIPS = "\033[0;31m{}select red ball:\033[0m"
BLUE_BALL_TIPS = "\033[0;34m{}select blue ball:\033[0m"
# 用户输入错误和超出范围的提示
READ = "--请输入[1-32]范围的整数"
BLUE = "--请输入[1-16]范围的整数"
# 用户选择复制后的提示
READ_REPEAT = "--红球{}已重复"
BLUE_REPEAT = "--蓝球{}已重复"
# 定义红/蓝球的空列表,接收用户最终的选择
use_red_choice = []
use_blu_choice = []
# while循环进行处理
count = 1
while count <= 8:
# 红球
if count <= 6:
# 接收用户的输入
use_entry1 = input(READ_BALL_TIPS.format([count])).strip()
# 用户只能输入整数,且不得超出范围
if len(use_entry1) == 0:
continue
elif not use_entry1.isdigit():
print(READ)
continue
elif int(use_entry1) > int(READ_BALL[-1]) or int(use_entry1) < int(READ_BALL[0]):
print(READ)
continue
elif int(use_entry1) in use_red_choice:
print(READ_REPEAT.format([int(use_entry1)]))
continue
else:
use_red_choice.append(int(use_entry1))
count += 1
# 蓝球
elif count >= 6:
# 接收用户的输入
use_entry2 = input(BLUE_BALL_TIPS.format([count])).strip()
# 用户只能输入整数,且不得超出范围
if len(use_entry2) == 0:
continue
elif not use_entry2.isdigit():
print(BLUE)
continue
elif int(use_entry2) > int(BLUE_BALL[-1]) or int(use_entry2) < int(BLUE_BALL[0]):
print(BLUE)
continue
elif int(use_entry2) in use_blu_choice:
print(BLUE_REPEAT.format([int(use_entry2)]))
continue
else:
use_blu_choice.append(int(use_entry2))
# 打印用户的最终选择结果
if count == 8:
# 打印用户的最终选择结果
print("红球:",use_red_choice,"\n蓝球:",use_blu_choice)
count += 1
三、效果