[英]Python Lottery generator I'm trying to figure out how to add a number input, and separate them on lines and be x amount of num long
# Lottery Generator
def lotto():
import random
integer = []
for number in range(0,5):
integer.append(random.randint(1,101))
return integer
print (lotto())
This is what I have so far. 到目前为止,这就是我所拥有的。 Not I try to input a number. 不,我尝试输入数字。 I can input a number and multiply it within the method, by using 我可以输入一个数字,并在方法中将其乘以
numInput = int(input("How many number sets do you need? "))
and adding that to the end 并将其添加到最后
print(lotto() * numInput)
but then it just gives me a number. 但这只是给我一个数字。 What I'm trying to get is something like this 我想要得到的是这样的
[5, 54, 84, 100, 72]
[16, 31, 95, 47, 10]
...etc. ...等等。 I want to print the output of lotto
for numInput
times. 我想将lotto
的输出打印numInput
次。
You need to loop over the amount of times inputted for numInput 您需要遍历为numInput输入的时间
def lotto():
import random
integer = []
for number in range(0,5):
integer.append(random.randint(1,101))
return integer
numInput = int(input("How many number sets do you need? "))
for i in range(0, numInput):
print (lotto())
If you would like to change the number of results returned per set. 如果要更改每组返回的结果数。 Then your function can accept an argument. 然后,您的函数可以接受参数。 For instance if you wanted 10 results per set. 例如,如果您希望每组10个结果。
def lotto(numbers):
import random
integer = []
for number in range(0,numbers):
integer.append(random.randint(1,101))
return integer
numInput = int(input("How many number sets do you need? "))
for i in range(0, numInput):
print (lotto(10))
Have your function take in a variable and then run a loop inside your function that appends that many of your lottery sets to a list and then return that list! 让您的函数接受一个变量,然后在函数内部运行一个循环,该循环将您的许多彩票集附加到列表中,然后返回该列表!
import random
def lotto(repetitions):
lista = []
for i in range(repetitions):
integer = []
for number in range(0,5):
integer.append(random.randint(1,101))
lista.append(integer)
return lista
number = int(input("How many sets: "))
print(lotto(number))
(xenial)vash@localhost:~/python/stack_overflow$ python3.7 lotto.py How many sets: 3 [[45, 99, 74, 64, 60], [65, 86, 51, 35, 93], [60, 4, 69, 14, 60]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.