Python 3.5 乐透程序帮助(简单)



嗨,这是我下面的python 3.5乐透程序

import random
one = (random.randint(1,40))
two = (random.randint(1,40))
three = (random.randint(1,40))
four = (random.randint(1,40))
five = (random.randint(1,40))
six = (random.randint(1,40))
seven = (random.randint(1,40))
#But it's going to print duplicate numbers. How to fix?
print ("The winning lotto numbers this week are:", one, two, three, four, five, six,"and", seven,)
print (input("Press the enter key to exit"))

如何防止程序在结果中打印重复的数字?

谢谢

你可以改用 random.sample 方法来创建一个列表,如下所示:

winners = random.sample(range(1,41), 7)
print ("The winning lotto numbers this week are: {}, {}, {}, {}, {}, {}, and {}".format(*winners))

您可以使用 random.sample

import random
numbers = range(1, 41)
draw = random.sample(numbers, 7)

最新更新