Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Library and Module Question – Python Beginner

I keep running into a few issues when trying to learn how to use the math module. I don’t know if the learning material is out of date or maybe I’m just doing something wrong, but every time an example of the math module being imported is used I can’t seem to keep up. Does anyone have any tips for using the math module in pyCharm?

Here is an example of what is going on with my current code;

from math import sqrt

print("Welcome to the Hypotenuse calculator!")

sideA = float(input("Please enter the length of side 'a': "))
sideB = float(input("Please enter the length of side 'b': "))

c = sqrt(sideA ** 2 = sideB ** 2)

print("Thank you! The length of the Hypotenuse is ", str(c))
    c = sqrt == sideA ** 2 = sideB ** 2
        ^
SyntaxError: cannot assign to comparison

Process finished with exit code 1

The code here is just a copy and paste from an example I was being shown because I was running into the same problems when trying to write it out myself, but this still doesn’t work.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your code:

from math import sqrt

print("Welcome to the Hypotenuse calculator!")

sideA = float(input("Please enter the length of side 'a': "))
sideB = float(input("Please enter the length of side 'b': "))

c = sqrt(sideA ** 2 = sideB ** 2)

print("Thank you! The length of the Hypotenuse is ", str(c))

revised code:

I had to import math for myself.
I then changed:

c = sqrt(sideA ** 2 = sideB ** 2)

to:

c = sqrt(sideA ** 2 + sideB ** 2)

Having "=" between sideA and sideB caused an error for me.

full code:

import math
from math import sqrt

print("Welcome to the Hypotenuse calculator!")

sideA = float(input("Please enter the length of side 'a': "))
sideB = float(input("Please enter the length of side 'b': "))

c = sqrt(sideA ** 2 + sideB ** 2)

print("Thank you! The length of the Hypotenuse is ", round(c,2)) #round() function will print any decimal places you want. Here it is set 2.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading