Python Forum

Full Version: help on random methods
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

I use the randint method which works fine

>>> import random
>>>
>>> def rand():
       for i in range(6):
              print(random.randint(1, 40))

>>> rand()
6
24
3
14
34
9
Now I try to use the sample methods but it returns no results

>>> import random
>>>
>>> def rand():
       for i in range(6):
              print(random.sample(range(40), 6)

>>> rand()
what is wrong please?
Missing a )
>>> import random
>>> 
>>> print(random.sample(range(40), 6))
[21, 5, 12, 37, 8, 20

>>> # Also when in REPL don't need print()
>>> random.sample(range(40), 6)
[5, 12, 0, 14, 35, 26]
As i mention before before when using function or code that can have many lines try not to use only use REPL.
In a script and running it would get SyntaxError: invalid syntax when missing ).
A better Editor like VS Code would also give more exact error message.
Output:
Expected ")" Pylance [7, 1]
(Apr-29-2021, 11:50 AM)snippsat Wrote: [ -> ]Missing a )
>>> import random
>>> 
>>> print(random.sample(range(40), 6))
[21, 5, 12, 37, 8, 20

>>> # Also when in REPL don't need print()
>>> random.sample(range(40), 6)
[5, 12, 0, 14, 35, 26]
As i mention before before when using function or code that can have many lines try not to use only use REPL.
In a script and running it would get SyntaxError: invalid syntax when missing ).
A better Editor like VS Code would also give more exact error message.
Output:
Expected ")" Pylance [7, 1]

oh perfect many thanks