Issue with math.sqrt not working

Hello everyone,

New to programming in Python, in my first class. I am having an issue with programming within IDEL.
In this example I am trying to perform math.sqrt and it is giving me a name error.
This is pretty much exactly like the textbook is showing me, so I am unsure what the issue is. I have done the following:
import sqrt from math, etc. The libraries are already there, so I don’t get it.
:
INPUT:
def hypotenuse(a1,b1):
ha = a12
hb = b1
2
hsquared = a12 + b12
result = math.sqrt(hsquared)
return result

After trying to run:
hypotenuse(3,4)

OUTPUT:
Traceback (most recent call last):
File “<pyshell#0>”, line 1, in
hypotenuse(3,4)
File “C:\Users\lglassman\AppData\Local\Programs\Python\Python311\practice.py”, line 19, in hypotenuse
result = math.sqrt(hsquared)
NameError: name ‘math’ is not defined

Thank you for any help you can provide. :slight_smile:
Lisa

If you’ve from math import sqrt, then you use the function directly e.g print(sqrt(12))

Only if you import the entire module would you need print(math.sqrt(12))

1 Like

Hello, @laglassman, and welcome to Python Software Foundation Discourse!

We hope you enjoy the discussions here, and your Python class as well.

You may be interested in looking at the following pages, which provide helpful information about this forum:

When you post Python code here, it is important to format it properly for display purposes. Note that the indentation of the code in your initial post and some of the operators that are part it are not properly displayed. To format Python code, copy it and paste it between two lines of backticks, which are sometimes referred to as fences, as follows:

```
def hypotenuse(a1,b1):
    ha = a1**2
    hb = b1**2
    hsquared = a1**2 + b1**2
    result = math.sqrt(hsquared)
    return result
```

When your hypotenuse function is formatted with fences as above, it appears as follows on the page that is posted:

def hypotenuse(a1,b1):
    ha = a1**2
    hb = b1**2
    hsquared = a1**2 + b1**2
    result = math.sqrt(hsquared)
    return result

You would, of course, place the entirety of the code you are posting between fences.

@rob42 mentioned the math module that you will need to import in order for the following statement within your code to execute properly:

    result = math.sqrt(hsquared)

For that statement to execute as you have written it, place this at the beginning of your code:

import math
1 Like

Hello Quercus! :slight_smile:

Thank you so much. That worked.
What I was doing was placing the import functions into the main IDLE interpreter window. I thought that would be where you would do it since it is already a python library.

I have shared this knowledge with the rest of my class in case they run into the same issue.

Thank you!!!

2 Likes

Thank you Rob,

I tried that in the wrong place. I got it now.
Your quick response was much appreciated! :smiley:

2 Likes

You’re welcome Lisa.

As a side note: there seems to be two popular views on the use of import

  1. Only import the tools that you need, rather than an entire module, as in:
    from math import sqrt
    sq_root = sqrt(x)

  2. Import the entire module so that you can see from which module a particular function is being sourced, as in:
    import math
    sq_root = math.sqrt(x)

In time, I’m sure you’ll come across this debate.

1 Like

To avoid subject “Issue with ** operator not working” there is quote from Python documentation:

The power operator binds more tightly than unary operators on its left; it binds less tightly than unary operators on its right. /…/

/…/ Thus, in an unparenthesized sequence of power and unary operators, the operators are evaluated from right to left (this does not constrain the evaluation order for the operands): -1**2 results in -1 .

So if math is already imported then pow could be option with less surprising results (note float though):

>>> import math
>>> math.pow(-2, 2)
4.0
>>> -2**2
-4
>>> (-2)**2
4
1 Like

in terms of memory usage, both methods import the whole module (not really all that much in a world of GB machines with swap space). the relevant different is how the namespace is changed and what the added names refer to. basically, method 1 does not reduce library work, but it does add a distinct place to check for name errors. i tend to use method 1 during development then refactor to method 2 afterwards if i have the time to do so. if i am writing the recipe to illustrate how to do something, i start with method 2 and leave it that way.

my code, my way; your code, your way. no need for a debate which is OT here.