我的 Lotto Python 脚本 运行 正确吗?

Is my Lotto Python script running correctly?

我写的脚本真的能正常运行吗?我试图模仿一个乐透选择场景,只是为了看看选择正确的数字实际上是不可能的(并向正在玩的朋友展示)。

乐透游戏的规则模仿了“EuroJackpot”游戏,您需要从 50 中选出 5 个,并且(!)从 10 个中选出 2 个。参见 here

这是我的脚本:

#!/usr/bin/python3

import random

given = [[8, 16, 12, 46, 47], [2, 4]]

def pickRandomNumbers():
    # source: whosebug.com/a/9755548/11160383
    fiveOutOf50 = random.sample(range(1, 50), 5)
    twoOutOf10 = random.sample(range(1, 10), 2)

    fiveOutOf50.sort()
    twoOutOf10.sort()

    return [fiveOutOf50, twoOutOf10]

# source: whosebug.com/a/61960902/11160383
def colored(r, g, b, text):
    return "3[38;2;{};{};{}m{} 3[38;2;255;255;255m".format(r, g, b, text)


solution = pickRandomNumbers()
while given != solution:
    if(given.__contains__(solution[0]) and given.__contains__(solution[1])):
        print(colored(0, 255, 0, solution))
    else:
        print(colored(255, 0, 0, solution))
        solution = pickRandomNumbers()

它有一个given,这是玩家选择的号码。一个 while 循环正在选择随机数,直到它与玩家的选择相匹配。我让它 运行 了很长时间,但显然从未匹配过...

它永远不会匹配,因为给定的数字是未排序的,而您正在对随机结果进行排序以匹配。

# first
given = [[8, 16, 12, 46, 47], [2, 4]] # the given 5 are unsorted
...
# get random numbers
tenOutOf50 = random.sample(range(1, 50), 5) 
twoOutOf10 = random.sample(range(1, 10), 2)

# and here is the problem
tenOutOf50.sort()
twoOutOf10.sort()

只是要指出,我并不是说你不能对随机选择进行排序...我只是指出为什么你可以 运行 终生拥有它。

编辑:您应该对 while 条件使用不同的方法。正如现在所写的,你永远不会看到真正的条件,因为它永远不会进入它......所以这段代码不会被执行:

if(given.__contains__(solution[0]) and given.__contains__(solution[1])):
    print(colored(0, 255, 0, solution))

我用一个简单的无限循环修复了我的 while 条件,并在找到解决方案后立即中断它: