Hello, I'm trying to create a program that calculates the hypotenuse and area of a right triangle.
I need to round the hypotenuse to 2 places after the decimal, and one place for the area

However, I can't seem to get it to work
If I tried
c = int(round(math.sqrt(a**2) + (b**2)), 2)
But I got this: TypeError: int() can't convert non-string with explicit base.

# This program computes right triangles

import math

print "This program computes the hypotenuse of a right triangle with the sides a and b."

a = input("Enter the length of the first leg to the nearest inch:  ")
b = input("Enter the length of the second leg to the nearest inch:  ")

# Calculate the length of the hypotenuse:

c = int(round(math.sqrt(a**2) + (b**2)))




# Calculate the area of the triangle
Area = int(round((a*b)/2))


# To create a line across:
print "-----------------------------------"
print "The hypotenuse is: ",c,"inches."
print "The area is: ", Area,"sq in."

Thanks!

You can use the "format" method

# This program computes right triangles

import math

print "This program computes the hypotenuse of a right triangle with the sides a and b."

a = input("Enter the length of the first leg to the nearest inch:  ")
b = input("Enter the length of the second leg to the nearest inch:  ")

# Calculate the length of the hypotenuse:

c = math.sqrt((a**2) + (b**2))


# Calculate the area of the triangle
Area = (a*b)/2.0


# To create a line across:
print "-----------------------------------"
print "The hypotenuse is: {c:.2f} inches.".format(c=c)
print "The area is: {area:.2f} sq in.".format(area=Area)

Hello, thank you so much for replying my thread.
So far I've learned very basic Python codes, so I can't include something I haven't learned in this program.

I appreciate you help!

Member Avatar for masterofpuppets

I think you have a mistake in the calculation for the hypothenuse. It should be a^2 + b^2 = c^2 => c = sqrt( a^2 + b^2 ) :)

# This program computes right triangles

import math

print "This program computes the hypotenuse of a right triangle with the sides a and b."

a = input("Enter the length of the first leg to the nearest inch: ")
b = input("Enter the length of the second leg to the nearest inch: ")

# Calculate the length of the hypotenuse:
c = round( math.sqrt( a**2 + b**2 ), 2 ) #where '2' is the argument specifying the number of digits after the decimal point

# Calculate the area of the triangle
Area = round( ( a * b ) / 2.0, 1 ) # same with '1' here

# To create a line across:
print "-----------------------------------"
print "The hypotenuse is: ", c, "inches."
print "The area is: ",  Area, "sq in."

note that you shouldn't convert the result to int because that automatically removes all digits after the decimal point. And also for the Area divide by 2.0 that does float division, otherwise you get no digits after the decimal point

hope that helps :)

I see...that explains why, I put too many parentheses.

Thank you so much! It helps a lot, now I understand more :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.