我开始编写列表中的前两个:
import random
def lottery_six():
setOfSix = set()
while len(setOfSix) < 6:
setOfSix.add(random.randint(1,49))
lottery = list(setOfSix)
return lottery
def generateLottery(lottery):
abc = set()
while (all(i >= 25 for i in lottery) == True) or (all(i < 25 for i in lottery) == True) or \
(sum(i >= 25 for i in lottery) >= 5) or (sum(i < 25 for i in lottery) >= 5):
abc = lottery_six()
return abc
print(generateLottery(lottery_six()))
然而,这并不奏效。为什么?我怎样才能修好它呢?在
你的代码似乎没有进入循环,你可以在while循环之前生成一个集合。此外,这些线路似乎是多余的:
(all(i >= 25 for i in lottery) == True) or (all(i < 25 for i in lottery) == True)
最终代码:
考虑到这一点,我们将重复这段代码,直到找到一组合适的值。首先,我们取范围[1,49],然后随机排序,取前6个值。如果满足第一个要求,则检查2。如果是这样,我们就打破循环,保留这个值列表。在
整个代码可以写成
^{pr2}$这将找到满足您条件的列表。最后一个语句有点密集,它会遍历列表中的每个值并检查是否至少有3个连续的值
x[ix+1] - i == 1 and x[ix+2] - x[ix+1] == 1
。如果这是真的,我们将值添加到列表中,如果在这个新列表的末尾至少有1个值,我们可以得出结论,至少有3个连续的值。在相关问题 更多 >
编程相关推荐