用python语言实现 30元钱买双色球_20.利用Python开发双色球选购的小程序(一)

该博客介绍了一个Python程序,用于实现双色球选号功能,确保用户选择不重复且在指定范围内。程序包含错误提示和用户输入验证,并在用户完成选择后展示所选号码。通过while循环处理红球和蓝球的选择,有效防止超出范围和重复输入的情况。
摘要由CSDN通过智能技术生成

一、需求

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

三、效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值