Basically, I need to calculate a hypotenuse of the right triangle
First, my code defines if the triangle is right or not, and then based on the lengths of two sides it calculates a hypotenuse of that triangle but it is not returning my h value which I need to return as a task of this exercise.
I can't understand what is a problem with returning h?
Why code is not returning it?
Thanks in advance
angle1 = input("what is a degree of the first angle? : ")
angle2 = input("what is a degree of the second angle? : ")
angle3 = input("what is a degree of the third angle? : ")
a1 = int(angle1)
a2 = int(angle2)
a3 = int(angle3)
length1 = input("what is a length of the first side? : ")
length2 = input("what is a length of the second side? : ")
l1 = int(length1)
l2 = int(length2)
def hypothenuse(a1, a2, a3, l1, l2):
if a1 != 90 or a2 != 90 or a3 != 90:
return ("\n sorry, but triangle sould be right -> one agle = 90 degrees")
else:
h = l1**2 + l2**2
return ("The hypothenuse of this triangle is equal to:", h)
hypothenuse(a1, a2, a3, l1, l2)
You are returning a value. The problem is that you are not telling Python to display it.
You can display variables, strings, bytes, integers and many other data types with print()
.
This is what you are after using:
print(hypothenuse(a1, a2, a3, l1, l2))
As mentioned in the comments you can store it in variables.
I would highly recommend you add "error catching" to your program, in case a user inputs a letter and not something that can be turned into an integer with int()
For example if under angle1
somebody entered a
you would get:
>>> a1 = int(angle1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'a'
>>>
A way to prevent this is to "catch" the error:
try:
a1 = int(angle1)
except ValueError:
print("Please enter an integer")
Don't worry if this is unfarmiliar right now, it will come up as you learn Python, and will become easy enough to understand.