彩票系统

该博客介绍了一个彩票购买系统程序。包含手选、机选函数用于购买彩票,中奖函数对比彩票与开奖结果得出红篮球相同个数,兑奖函数计算具体金额。还设有程序菜单,可进行购买、查看开奖等操作,同时计量购买数量和中奖总金额。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import random

手选函数

def shoudong():
lottery_buy = []
i = 1
while len(lottery_buy) < 7:
# 1.根据输入的球的个数,来区分红球还是蓝球
if i <= 6:
# 输入的数字是以字符串形式存在的
num = input(‘请输入第{}个红球号码:’.format(i))
# 需要将字符串转换为int
num = int(num)
# 1.判断这个号码是否在合理的范围内[1,32],
if 1 <= num <= 32:
# 2.判断列表中是否已经存在该号码
if num in lottery_buy:
print(‘该号码已存在,请重新选择’)
else:
lottery_buy.append(num)
i += 1
else:
print(‘你输入的第{}个红球号码有误,请重新输入’.format(i))

    else:
        # 在红球全搞定的情况下,对所有的红球进行排序,sort(),默认按自然顺序排(从小到大)
        lottery_buy.sort()
        num = int(input('请输入蓝球号码'))
        if 1 <= num <= 16:
            lottery_buy.append(num)
        else:
            print('蓝球号码有误,再来一次')
return lottery_buy

机选函数

def zidong():
l = []
i = 1
while i < 8:
if i < 7:
num = random.randint(1, 32)
if num in l:
continue
else:
l.append(num)
i += 1
l.sort()
else:
num = random.randint(1, 16)
l.append(num)
i += 1
return l

中奖函数,将彩票与开奖结果对比,得出红篮球相同的个数

def zhongjiang(l1,l2):
num1 = 0
num2 = 0
for i in range(len(l1)):
if i < 6:
if l1[i] in l2[0:6]:
num1 += 1
else:
if l1[i] == l2[i]:
num2 += 1
# print(‘红球中奖个数为’, num1)
# print(‘蓝球中奖个数为’, num2)
return [num1,num2]

兑奖函数得出具体金额

def money(num1,num2):
if num1 == 6 and num2 == 1:
return 5000000
elif num1 == 6 and num2 == 0:
return 300000
elif num1 == 5 and num2 == 1:
return 3000
elif num1 == 5 and num2 == 0 or num1 == 4 and num2 == 1:
return 200
elif num1 == 4 and num2 == 0 or num1 == 3 and num2 == 1:
return 10
elif num1 == 2 and num2 == 1 or num1 == 1 and num2 == 1 or num1 == 0 and num2 == 1:
return 5
else:
return 0

开始程序的菜单

def show_menu():
print(‘欢迎访问彩票购买系统v1.0’)
print(‘1.购买彩票’)
print(‘2.查看开奖’)
print(‘3.退出系统’)

定义一个空列表来放置所有彩票

caipiao = []

计量购买彩票的数量

num = 0

计量中奖的总金额

total_money = 0
while True:
show_menu()
choice = input(‘请选择1-3:’)
# b为开奖结果
b = zidong()
# 定义空列表存放所有彩票
if choice == ‘1’:
# 购买彩票的操作
num = int(input(‘请输入购买的彩票注数:’))
choice3 = input(‘请问手选:1,还是机选:2’)
if choice3 == ‘1’:
for i in range(num):
a = shoudong()
caipiao.append(a)
print(f’你的第{i+1}注号码为{a}’)

    elif choice3 == '2':
        for i in range(num):
            a = zidong()
            caipiao.append(a)
            print(f'你的第{i+1}注号码为{a}')
    else:
        break
# 查看看开奖结果
elif choice == '2':
    print(f'中奖号码为{b}')
    if num == 0:
        print('请先购买彩票')
    else:
        for i in range(num):
            # c为引用中奖函数进行对比,返回的是红球中奖个数及蓝球中奖个数
            c = zhongjiang(b,caipiao[i])
            # result为引用兑奖函数显示中奖结果,返回的是中奖金额
            result = money(c[0],c[1])
            total_money += result
            print(f'第{i+1}注彩票中:红球中{c[0]}个,蓝球中{c[1]},中奖金额为{result}')
        print(f'花费{2*num}元,中奖{total_money}')
        caipiao.clear()
        total_money = 0
elif choice == '3':
    print('感谢为中国福利事业做出的贡献,欢迎下次再来')
    break
else:
    print('输入有误,再来一次')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值