samedi 5 novembre 2022

random numbers (using random.randint) returning None [duplicate]

I'm trying to get a random number that is not used (already printed). here's my code:

import random
used_numbers = []

def generate_random_number():
    num = random.randint(1, 90)
    if num not in used_numbers:
        used_numbers.append(num)
        return num
    elif num in used_numbers and num == generate_random_number():
        generate_random_number()

for i in range(90):
    print(generate_random_number())

For some reason after like 20 numbers it returning "None"..

Even if I tried switching to set, I'm still getting alot of "None"s.

What I'm Expecting to get:

A random number from 1 to 90 that is not used, for example:

1
90
82
53
34
65
21
86

ex.

Before I was getting something like this:

34
34
34
12
53
34
12
12
17
18
10
23
12

ex.




Aucun commentaire:

Enregistrer un commentaire