题目要求:
1.先让用户依次选择6个红球,再选择2个蓝球,最后统一打印用户选择的球号
2.确保用户不能选择重复的,选择的数不能超出范围
源码如下:
# 定义两个双色球空列表
red_ball_list = []
blue_ball_list = []
print('Welcome to 小猿圈 lottery station\n')
n = 1
while 1:
red = int(input(f'[{n}]select red ball:'))
if red < 1 or red > 32:
print('only can select n between 1-32')
continue
if red in red_ball_list:
print(f'number {red} already exist in red ball list.')
else:
red_ball_list.append(red)
n += 1
if len(red_ball_list) == 6:
n = 1
break
while 1:
blue = int(input(f'[{n}]select blue ball:'))
if blue < 1 or blue > 16:
print('only can select n between 1-16')
continue
if blue in blue_ball_list:
print(f'number {blue} already exist in blue ball list.')
else:
blue_ball_list.append(blue)
n += 1
if len(blue_ball_list) == 2:
break
# 输出双色球
print(f'Red ball: {red_ball_list}')
print(f'Blue ball: {blue_ball_list}')
print('Good Luck')
运行结果如下: