Python Forum

Full Version: Lottery generator (beginner)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello!
I just started to learn python and I'm a bit lost at the moment.
Also new here, so correct me if and when I'm doing something wrong.

I'm trying to make lottery rows that compare themselves to those already existing.
Decided to ask here, because I don't even know how to google this problem.
Thank you for your help in advance.

#Code here below
import random

list=[]
def lotto():
    for i in range(10):
        r=random.randint(1,100)
        if r not in list: list.append(r)

a = 1
while a < 10:
    lotto()
    print(list)
    a += 1
first three lines of the output are:
Output:
[50, 8, 14, 96, 25, 79, 66, 61, 31] [50, 8, 14, 96, 25, 79, 66, 61, 31, 40, 19, 56, 37, 27, 29, 58, 74] [50, 8, 14, 96, 25, 79, 66, 61, 31, 40, 19, 56, 37, 27, 29, 58, 74, 11, 39, 67, 41, 33, 60, 26, 24]
First of all, you shouldn't name a list 'list'. The word 'list' is a built-in type/function in Python. If you name your list 'list', you no longer have access to the built-in. This may cause problems if you are using/importing other people's code. Call your list something like 'numbers', which describes what's in the list.

Second, you need to define the list in the function, so that it gets reset every time the function is called. As it is, you always appending to the same list. That's why your output gets longer and longer each time.

Third, moving the definition inside the function will make it unavailable outside the function. You will need to use the return statement to return it from the function, and you will need to assign the function call on line 11 to a variable. Details on how to do this are in the functions tutorial link in my signature below.
Thank you!
With this it worked better, but I'll continue learning!

import random
def lotto():
    numbers=[]
    for i in range(10):
        r=random.randint(1,100)
        if r not in numbers: numbers.append(r)
    print(numbers)

a = 1
while a < 10:
    lotto()
    a += 1
Output:
[91, 55, 59, 47, 67, 23, 37, 75, 74, 89] [94, 15, 25, 55, 72, 21, 79, 6, 42] [74, 24, 49, 81, 56, 94, 37, 23, 34, 1] [34, 23, 42, 71, 35, 89, 62, 70, 28, 84] [89, 27, 79, 99, 41, 77, 50, 40, 21, 28] [68, 59, 51, 92, 50, 26, 6, 52, 28, 77] [62, 56, 85, 66, 4, 90, 65, 52, 26] [3, 27, 23, 71, 62, 97, 40, 91, 66, 54] [47, 19, 66, 55, 90, 53, 86, 95, 43, 65]
Shouldn't random.sample be more suitable for task at hand?

>>> import random
>>> help(random.sample)
Help on method sample in module random:

sample(population, k) method of random.Random instance
    Chooses k unique random elements from a population sequence or set.
    
    Returns a new list containing elements from the population while
    leaving the original population unchanged.  The resulting list is
    in selection order so that all sub-slices will also be valid random
    samples.  This allows raffle winners (the sample) to be partitioned
    into grand prize and second place winners (the subslices).
    
    Members of the population need not be hashable or unique.  If the
    population contains repeats, then each occurrence is a possible
    selection in the sample.
    
    To choose a sample in a range of integers, use range as an argument.
    This is especially fast and space efficient for sampling from a
    large population:   sample(range(10000000), 60)
(END)
So you just can write:

>>> for i in range(10):
...     print(random.sample(range(1, 100), 10))
...