为什么我的代码没有返回斜边的值?
Why my code is not returning the value of hypotenuse?
基本上,我需要计算直角三角形的斜边
首先,我的代码定义三角形是否正确,然后根据两侧的长度计算该三角形的斜边,但它不是 return 我需要 return 的 h 值] 作为本练习的任务。
我不明白 returning h?
有什么问题
为什么代码没有 returning 呢?
提前致谢
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)
您正在返回一个值。问题是你没有告诉 Python 显示它。
您可以使用print()
显示变量、字符串、字节、整数和许多其他数据类型。
这是你使用后的样子:
print(hypothenuse(a1, a2, a3, l1, l2))
如评论中所述,您可以将其存储在变量中。
我强烈建议您将 "error catching" 添加到您的程序中,以防用户输入一个字母而不是可以用 int()
转换为整数的东西
例如,如果在 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'
>>>
防止这种情况的一种方法是 "catch" 错误:
try:
a1 = int(angle1)
except ValueError:
print("Please enter an integer")
如果现在对此不熟悉,请不要担心,随着您的学习,它会出现 Python,并且会变得足够容易理解。
基本上,我需要计算直角三角形的斜边
首先,我的代码定义三角形是否正确,然后根据两侧的长度计算该三角形的斜边,但它不是 return 我需要 return 的 h 值] 作为本练习的任务。
我不明白 returning h?
有什么问题
为什么代码没有 returning 呢?
提前致谢
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)
您正在返回一个值。问题是你没有告诉 Python 显示它。
您可以使用print()
显示变量、字符串、字节、整数和许多其他数据类型。
这是你使用后的样子:
print(hypothenuse(a1, a2, a3, l1, l2))
如评论中所述,您可以将其存储在变量中。
我强烈建议您将 "error catching" 添加到您的程序中,以防用户输入一个字母而不是可以用 int()
例如,如果在 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'
>>>
防止这种情况的一种方法是 "catch" 错误:
try:
a1 = int(angle1)
except ValueError:
print("Please enter an integer")
如果现在对此不熟悉,请不要担心,随着您的学习,它会出现 Python,并且会变得足够容易理解。