Solutions For NameError: name ‘sqrt’ is not defined in Python

NameError: name 'sqrt' is not defined in Python

Are you annoyed by the NameError: name ‘sqrt’ is not defined in Python? This post will discuss the causes of this error. We’ll also discuss the sqrt() function and how to avoid making this error again.

What is the sqrt() function?

The sqrt() function calculates the square root of a number. For instance, to find the square root of 9, we would use the following code:

sqrt(9)

It would return: 3

Causes of the NameError: name ‘sqrt’ is not defined in Python

Use a function before importing

When you try to use a function that hasn’t been imported into your program, Python throws a NameError, as shown in the following example.

num = 9

# Use a function that hasn't been imported
result = sqrt(num)
print(result)

Error:

NameError: name 'sqrt' is not defined

Make a typo when defining

You may be working with an unassigned variable if you make a typo when defining a variable or function. This is the cause of the error. For example:

# Make a typo when defining a function
def sprt(num):
  print(f'sqrt({num})')

# Try to use sqrt
print(sqrt(9))

Error:

NameError: name 'sqrt' is not defined

Call a function before declaring it

Functions prevent us from repeating code. When using functions, one rule to remember is not to call the function before declaring it. Python will throw a NameError if you attempt to call a function before declaring it. As an example:

# Try to use sqrt()
print(sqrt(9))

# Declare the sqrt() function
def sqrt(num):
  result = num ** 0.5
  return result

Error:

NameError: name 'sqrt' is not defined

How to fix this error?

Import the Math module before using the sqrt() function

It is important to remember to import a module before using it. Ensure to import math before using the sqrt() function in the Math module!

# Import Math module first
from math import sqrt

# Then, use the sqrt() function
print(sqrt(9))

Output:

3.0

Using the custom sqrt() function

You can write your sqrt() function without importing any external libraries. The square root of num can be easily calculated using the code num**0.5.

Remember to check for typos and to declare the function before using it.

# Declare the sqrt() function
def sqrt(num):
  return num ** 0.5

# Then, use the sqrt() function
print(sqrt(9))

Output:

3.0

How to avoid getting NameError in the future?

Making your code well-organized and easy to read is one of the most important things you can do to avoid NameErrors. Use clear variable and function names, and comment on your code. These things will help you in detecting errors early before they cause problems.

Summary

In summary, importing the Math module and using the custom sqrt() function will resolve the NameError: name ‘sqrt’ is not defined. We can avoid this error in the future by importing all required modules and using clear variable names. If you have any questions, please leave them in the comment section below.

Happy coding!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *