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 = b12
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
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:
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.
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):
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.