Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+4 votes

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

asked in CSC201 Spring 2021 by (1 point)

6 Answers

+4 votes
 
Best answer

make sure to include

import random

at the top of your code

answered by (1 point)
selected by
+1

Thank you!

+3 votes

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

answered by (1 point)
+1

Thank you!

+3 votes

Yes you need to import it before using it

import random

just like with math too

answered by (1 point)
+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 by (1 point)
+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 by (1 point)
+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 by (1 point)
+1

thank you!

...