Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Python by (16.4k points)
closed by

I'm having issues with Python's import random capacity (function). It appears to be that import random and from irregular import random are bringing in various things. I'm at present utilizing Python 2.7.3

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32

Type "copyright", "credits" or "license()" for more information.

>>> random()

Traceback (most recent call last):

File "<pyshell#0>", line 1, in <module>

    random()

NameError: name 'random' is not defined

>>> random.randint(1,5)

Traceback (most recent call last):

File "<pyshell#1>", line 1, in <module>

random.randint(1,5)

NameError: name 'random' is not defined

>>> import random

>>> random()

Traceback (most recent call last):

File "<pyshell#3>", line 1, in <module>

random()

TypeError: 'module' object is not callable

>>> random.randint(1,5)

2

>>> from random import random

>>> random()

0.28242411635200193

>>> random.randint(1,5)

Traceback (most recent call last):

File "<pyshell#7>", line 1, in <module>

random.randint(1,5)

AttributeError: 'builtin_function_or_method' object has no attribute 'randint'

>>> 

closed

4 Answers

0 votes
by (15.4k points)
 
Best answer
In Python 2.7.3, there seems to be confusion with the random function when using different import statements. When you try to call random() or random.randint(1, 5), it raises a NameError because the name 'random' is not recognized.

To resolve this, you can employ one of the following solutions:

Import the random module using import random and use the random.random() and random.randint(1, 5) syntax to call the functions.

If you prefer to directly call the random() function without the module name prefix, you can use from random import random. However, note that this approach won't provide access to other functions like randint.

By selecting the appropriate import statement, you can fix the issue and utilize the random function correctly in Python 2.7.3.
0 votes
by (26.4k points)

Here, import random imports the random module, which contains an assortment of activities with random number generation. Among these is the random() work, which produces irregular numbers somewhere in the range of 0 and 1. 

Doing the import this way this expects you to utilize the language structure random.random().

Use the below code to import the random function:

from random import random

Want to learn python to get expertise in the concepts of python? Join python certification course and get certified

0 votes
by (25.7k points)
To address the issue you're facing with the random function in Python 2.7.3, you can try the following solutions:

When using import random, you need to prefix the function calls with the module name random like random.random() and random.randint(1, 5).

Example:

import random

random.random()

random.randint(1, 5)

If you prefer to use from random import random, you can directly call random() without the module name prefix. However, note that this approach won't provide access to other functions like randint.

Example:

from random import random

random()

By following either of these approaches, you should be able to use the random function successfully and generate random numbers as desired.
0 votes
by (19k points)
You can resolve the issue with the random function in Python 2.7.3 by trying the following solutions:

To use the random function after importing the module, make sure to prefix the function calls with the module name random. For example, you can use random.random() and random.randint(1, 5).

Alternatively, if you prefer to import only the random function, you can use from random import random. This allows you to directly call random() without the need for the module name prefix. However, note that this approach doesn't provide access to other functions like randint.

By following these approaches, you can resolve the issue and successfully use the random function to generate random numbers in Python 2.7.3.

Related questions

0 votes
1 answer
asked Sep 25, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Sep 17, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Feb 26, 2021 in Python by laddulakshana (16.4k points)
0 votes
1 answer
asked Sep 26, 2019 in Python by Sammy (47.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...