利用Python开发双色球选购小程序

需求及代码

# -*- 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)

代码执行结果

转载于:https://www.cnblogs.com/chenliangc/articles/11489110.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值