繁体   English   中英

为什么我的代码没有返回斜边的值?

[英]Why my code is not returning the value of hypotenuse?

基本上,我需要计算直角三角形的斜边
首先,我的代码定义了三角形是否正确,然后根据两侧的长度,计算了该三角形的斜边,但它没有返回我的h值,我需要将其返回作为本练习的任务。 我不明白返回h有什么问题?
为什么代码没有返回?

提前致谢

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))

如注释中所述,您可以将其存储在变量中。

我强烈建议您在程序中添加“错误捕获”,以防用户输入字母而不是可以通过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'
>>>

一种防止这种情况的方法是“捕获”错误:

try:
    a1 = int(angle1)
except ValueError:
    print("Please enter an integer")

不用担心,这现在还不是很普遍,它会在您学习Python时出现,并且变得足够容易理解。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM