[英]Why my code is not returning the value of hypotenuse?
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. 首先,我的代码定义了三角形是否正确,然后根据两侧的长度,计算了该三角形的斜边,但它没有返回我的h值,我需要将其返回作为本练习的任务。 I can't understand what is a problem with returning h?
我不明白返回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.
问题是您没有告诉Python显示它。
You can display variables, strings, bytes, integers and many other data types with print()
. 您可以使用
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()
我强烈建议您在程序中添加“错误捕获”,以防用户输入字母而不是可以通过
int()
转换为整数的内容
For example if under angle1
somebody entered a
you would get: 例如,如果有人在
angle1
下输入a
您将得到:
>>> 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. 不用担心,这现在还不是很普遍,它会在您学习Python时出现,并且变得足够容易理解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.