mardi 16 juin 2020

Python function default argument random value

In the following code, a random value is generated as expected:

import random

for i in range(10):
    print(random.randint(0,10))

However, this does not work if I use a function:

import random

def f(val: int = random.randint(0,10)):
    print(val)

for i in range(10):
    f()

Why is the result of the second code snippet always the same number? The most similar question I could find is this one, but it refers to a different language (I don't master) .




Aucun commentaire:

Enregistrer un commentaire