lundi 17 mai 2021

Function Output fails

I'm newbie with a newbie question that couldn't find the answer (here or on Google).

I'm learning write functions and don't understand why this happening.

The code:

#func

def draw(n):
    import random
    nbers=(random.sample(range(1,101), n))
    print(nbers)

ns=int(input("How many numbers you want?"))

draw(ns)
print(nbers)

The log:

 How many numbers you want?5
[57, 10, 76, 46, 66]

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-16-50675fd81745> in <module>
      9 
     10 draw(ns)
---> 11 print(nbers)

NameError: name 'nbers' is not defined

The question:

Why the print output (inside function) works, but if I call the list produced (printing the list again), it will be empty?

How can I use the output if the list has numbers in the first moment and when I print it again the numbers are gone?

What's happening here?

Thank you for your help!

Ludwig




Aucun commentaire:

Enregistrer un commentaire