Home > Blockchain >  Library and Module Question - Python Beginner
Library and Module Question - Python Beginner

Time:11-16

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.

CodePudding user response:

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.
  • Related