Search code examples
pythonpythagorean

Why isn't this Python pythagorean theorem solver working?


print "Welcome to Dylan's Pythagorean Theorem Solver."

from math import sqrt 

print "What are we solving for? A hypotenuse or a leg?"

ask = raw_input("# ")

if ask == "hypotenuse":
    print "What is the value of your first leg?"

    leg1 = raw_input("# ")

    print "The value of your first leg is %s. What is the value of your second leg?" % (leg1)

    leg2 = raw_input("# ")    

    print "The length of your hypotenuse is "sqrt((leg1 ** 2) + (leg2 ** 2))

Solution

  • I'm a beginner but this is how I got your code to work:

    Raw_input produces strings. You need to convert leg1 and leg2 to ints or floats before you can use them in sqrt. You can do that like this:

    leg1 = int(input("# "))
    

    You've got the same problem but in reverse in print (python is expecting a str but getting a float). You're also missing an operator in print.

    It might be easier to just create a new variable for the result of sqrt, convert it to a str, and then use the variable in print.

    hypotenuse = str(sqrt((leg1 ** 2) + (leg2 ** 2)))