【发布时间】:2018-03-19 05:23:49
【问题描述】:
- 如果有五个“短号”或所有六个都是“短号”(短号为 1
- 如果有五个“大数”或所有六个都是“大数”(大数定义为 25
- 如果六个数字中至少有五个是偶数
- 如果六个数字中至少有五个是奇数
- 如果至少三个数字是连续的数字(例如 [13, 14, 15, 28, 35, 49] --> 绘制新的六。或者另一个示例是例如 [5, 6, 7, 8, 21 , 38] --> 绘制新的六个数字)
我开始编写列表中的前两个:
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()))
但是,这不起作用。为什么?我该如何解决?
【问题讨论】:
-
如果满足这些条件,你就赢了?
-
你用了一个集合,你是不是假设同一个值不能重复?这样的列表像 [1,1,1,1,1,1] 是不允许的?列表必须都是唯一值?
-
是的,数字必须是唯一的。
-
不,只是要生成一个满足上述要求的集合。
-
上面的1个,因为条件1和2是互斥的,对吧?