Python 3.5 Lotto 程序帮助(简单)

标签 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"))

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

谢谢

最佳答案

您可以使用random.sample方法,创建一个像这样的列表:

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

关于Python 3.5 Lotto 程序帮助(简单),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42882706/

相关文章:

python - 为什么协议(protocol)必须以大端顺序发送到套接字函数而其余参数则不是?

C++:为什么这个方法每次都返回相同的随机数?

numpy - 指定numpy的multivariate_normal随机采样中的球面协方差

python - Python中复杂多重继承中的构造函数调用序列

python - Python OpenCV cap.read()解压缩帧?

python - Python中随机数的长尾分布

c++ - C++ 中的 rand() 是可预测的吗

unit-testing - 如何对 Racket 中具有动态输出的函数使用单元测试框架?

linux - Windows 下 Keys 的随机数生成器 - 从 Linux 移植

Python:从元组数组中提取具有最大/最小第n个元素的元组