通过调用random模块、定义函数、使用循环来简单模拟彩票大奖有多难中奖!这里我根据是否需要考虑号码顺序分成了两种情况。在不超过规定的最大抽奖次数的情况下,打印出循环了多少次才中奖或者一直没有中奖。
大致分为4个步骤:
1.定义中奖号码(假设是4位数)
2.定义彩票号码
3.识别是否中奖
4.导入数据,进行判断并输出结果(可以设置最大抽奖次数)
考虑号码顺序
#考虑号码顺序
from random import randint
def win(possibilities):
"""定义中奖号码"""
winning_ticket = []
while len(winning_ticket) < 4:
one = choice(possibilities)
if one not in winning_ticket:
winning_ticket.append(one)
return winning_ticket
def choose(possibilities):
"""定义彩票号码"""
choose_ticket = []
while len(choose_ticket) < 4:
one = choice(possibilities)
if one not in choose_ticket:
choose_ticket.append(one)
return choose_ticket
def identify(winning_ticket, choose_ticket, order = False):