import random
from lotteryH import isWhole
t1 = '开始游戏'
t2 = '结束游戏'
print(t1.center(50,'*'))
money = 100
rou = 1
print('您的初始余额为 %d 元。' % money)
while 1:
print('第 %d 轮游戏:' % rou)
result_r = random.sample(range(1,34),6)
result_r.sort()
result_b = random.sample(range(1,17),1)
result = result_r + result_b
while 1:
guess = []
guess_r = []
guess_b = []
num = 1
while 1:
print('===购买第 %d 注彩票===' % num)
while 1:
temx = eval(input('请输入6个红色球号码(1~33):'))
tem_r = list(temx)
# 错误处理
if len(tem_r)==6 and isWhole(tem_r) and max(tem_r)<=33 and min(tem_r)>=1:
guess_r.append(tem_r)
break
else:
print('请按要求输入号码!')
continue
while 1:
temy = eval(input('请输入1个蓝色球号码(1~16):'))
if len([temy])==1 and type(temy) == int and temy >=1 and temy<=16 :
tem_b = [temy]
guess_b.append(tem_b)
break
else:
print('请按要求输入号码!')
continue
tem = tem_r + tem_b
guess.append(tem)
money = money - 2
print('您的余额为 %d 元。' % money)
ans = input('是否继续购买(y/n):')
if ans=='y' and money > 0:
num += 1
continue
elif money <= 0:
print('您的余额已不足,已为您自动开奖。')
break
else:
break
print('*****************************************************')
print(f'本次中奖号码为:红球 {result_r},蓝球 {result_b}')
print('*****************************************************')
print('您所选的号码为:')
for i in range(len(guess)):
if guess[i] == result:
price = '一'
money = money + 5000000
elif guess_r[i] == result_r:
price = '二'
money = money + 900000
elif len(set(guess_r[i])&set(result_r))==5 and guess_b[i] == result_b:
price = '三'
money = money + 3000
elif len(set(guess_r[i])&set(result_r))==5 or (len(set(guess_r[i])&set(result_r))==4 and guess_b[i] == result_b):
price = '四'
money = money + 200
elif len(set(guess_r[i])&set(result_r))==4 or (len(set(guess_r[i])&set(result_r))==3 and guess_b[i] == result_b):
price = '五'
money = money + 10
elif guess_b[i] == result_b:
price = '六'
money = money + 5
else:
price = '无'
print(f'红球 {guess_r[i]},蓝球 {guess_b[i]}',end=' ')
if price == '无':
print('未中奖')
else:
print(f'{price}等奖!')
break
ans2 = input('是否继续游戏(y/n):')
if ans2 == 'y':
rou+=1
continue
else:
print(t2.center(50, '*'))
break
说明:isWhole()函数是用于检测一个列表里的元素是否均为整数,是返回Ture,否则返回False。目的是用于检测用户输入的号码是否符合要求。自己写的,大家可以自己尝试写一下。
游戏规则:初始金额为100元,每次购买彩票需支付2元,一次可购买多组号码同时进行开奖,不同奖项设置不同金额奖金,最高可达500W元!!!。购买完毕后自动进行开奖,显示中奖号码和用户购买的号码,在每组号码末尾均注明是否中奖。