Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help on random methods
#1
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?
Reply
#2
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]
Reply
#3
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  random methods Skaperen 16 2,638 Sep-28-2022, 10:44 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020