import random
unionLotto=[]
userList=[]
user_blueball = ''
computer_blueball = ''
# 判断是否位数字
def isNum(str):
try:
a = eval(str)
if type(a) == type(1) or type(a) == type(1.0) or type(a) == type(1 + 1j):
return True
else:
return False
except:
return False
# 生成红色球六个
while True:
num = str(random.randint(1, 33))
if (len(num) == 1):
num = '0{}'.format(num)
if num in unionLotto:
continue
unionLotto.append(num)
if len(unionLotto) == 6:
break
unionLotto.sort()
# 生成蓝色球1个
num = str(random.randint(1, 16))
if (len(num) == 1):
num = '0{}'.format(num)
computer_blueball = num
# 打印结果
print('本期双色球的中奖号码为:', end='')
for num in unionLotto:
print('\t' + num + '\t', end='')
print('\t{}'.format(computer_blueball), end='')
print('\n最后一位为蓝色球')
print('红色球(1~33)')
# 循环接受用户输入的值
while True:
num = input('请输入第{}位红色球: '.format(len(userList)+1))
if num == '':
print('你输入的数字为空,请重新输入!')
continue
if not isNum(num):
print('请输入数字!')
continue
if int(num) > 33 or int(num) < 1:
print('你输入的数字超出范围,请重新输入!')
continue
if (len(num) == 1):
num = '0{}'.format(num)
if num in userList:
print('该数字已经存在,请重新输入!')
continue
userList.append(num)
if len(userList) == 6:
break
userList.sort()
while True:
num = input('请输入蓝色球(1~16): ')
if int(num) > 16 or int(num) < 1:
print('输入有误,请重新输入')
continue
if (len(num) == 1):
num = '0{}'.format(num)
user_blueball = num
if user_blueball != '':
break
# 打印结果
print('你购买的号的号码为:', end='')
for num in userList:
print('\t{}\t'.format(num), end='')
print('\t{}'.format(user_blueball), end='')
print()
# 判断是否中奖
count = 0
for number in userList:
if number in unionLotto:
count += 1
if count != 0:
print("您买中{}了个红色球".format(count))
if user_blueball == computer_blueball:
print("你买中了蓝色球")
else:
print("你没有买中蓝色球")
if count == 6 and user_blueball == computer_blueball:
print("一等奖")
elif count == 6:
print("二等奖")
elif count == 5 and user_blueball == computer_blueball:
print("三等奖")
elif count == 5 or count == 4 and user_blueball == computer_blueball:
print("四等奖")
elif count == 4 or count == 3 and user_blueball == computer_blueball:
print("五等奖")
elif user_blueball == computer_blueball:
print("六等奖")
else:
print("没有中奖")
Python 双色球模拟
于 2024-01-25 15:52:20 首次发布
本文介绍了一个用Python编写的程序,模拟双色球抽奖过程,包括随机生成红色球和蓝色球,接受用户输入并判断是否中奖。
摘要由CSDN通过智能技术生成