I'm trying to produce a random integer n
, and create a list of n random integers with values between 0 and 9.
Here is my code:
def randomNumbers(n): myList = [] needMoreNumbers = True while (needMoreNumbers): randomNumber = int(random.random() * 10) myList.append(randomNumber) n = n -1 if (n < 1): needMoreNumbers = False return myList
When I run it, it says:
NameError: global name 'random' is not defined
The Python "NameError: name 'randint' is not defined" occurs when we use the randint function without importing it first. To solve the error, import the randint function before using it - from random import randint . Here is an example of how the error occurs. Copied!
Using 'import random' imports the package, which you can then use the function from this package: 'random. random()'. You can use any other function from the 'random' package as well. You can also tell python to specifically import only the random function from the package random: 'from random import random'.
You haven't imported random
module. Add this to the top of your script:
import random
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With