Welcome to the CSC Q&A.
Get help and give help!
Write great code!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+3 votes

I was receiving the error "NameError: name 'random' is not defined" after using "random.randrange(...)"

asked ago in CSC201 Spring 2021 by (1.4k points)

6 Answers

+4 votes
 
Best answer

make sure to include

import random

at the top of your code

answered ago by (1.5k points)
selected ago by
+1

Thank you!

+3 votes

You need to import random
You can't use built in functions without importing it

answered ago by (1.1k points)
+1

Thank you!

+3 votes

Yes you need to import it before using it

import random

just like with math too

answered ago by (1.2k points)
+1

thank you!

+2 votes

The reason I was receiving the error was because I forgot to do "import random" before doing "random.randrange(...)". Hence the error saying 'random' is not defined.

answered ago by (1.4k points)
+2 votes

Hi,
I think you forgot to put "import random" at the top of the code like even before def main().

Thank you and i know most of them answered the same thing! So, ya! hehe.

Have a good day!

answered ago by (2.1k points)
+1

Gotta get house points somehow xD

+1

HAHA, True xD

+2 votes

Include 'Import random' at the top of your program and that will work.
Hope this helps!

answered ago by (1.1k points)
+1

thank you!

...