Python 3.5 Lotto程序帮助(简单)

时间:2017-03-19 03:47:25

标签: python random

嗨,这是我的以下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"))

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

谢谢

2 个答案:

答案 0 :(得分:1)

您可以使用random.sample方法来创建如下列表:

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

答案 1 :(得分:0)

您可以使用random.sample

import random

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