Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用if-else语句查找三角形的面积_Python_Python 3.x - Fatal编程技术网

Python 使用if-else语句查找三角形的面积

Python 使用if-else语句查找三角形的面积,python,python-3.x,Python,Python 3.x,我应该编写一个程序,提示用户输入三角形三条边的长度,确定这三条长度可以形成一个三角形,如果是,则使用Heron公式计算面积,精确到4位数。这是我目前掌握的,我不知道在数学中放在哪里或如何放 import math def main(): print() print("Triangle Area Program") print() a, b, c = eval(input("Enter three lengths separated by commas: "))

我应该编写一个程序,提示用户输入三角形三条边的长度,确定这三条长度可以形成一个三角形,如果是,则使用Heron公式计算面积,精确到4位数。这是我目前掌握的,我不知道在数学中放在哪里或如何放

import math
def main():
    print()
    print("Triangle Area Program")
    print()
    a, b, c = eval(input("Enter three lengths separated by commas: "))
    print()
    s = (a+b+c) / 2.0
    area = sqrt(s*(s-a)*(s-b)*(s-c))
    if a > b:
        a, b = b, a
    if a > c:
        a, c = c, a
    if b > c:
        b, c = c, b
    else:
        a + b > c
        print("A triangle cannot be formed.")

main()

下面是一个稍微修改过的程序版本,它检查输入是否与一个复合条件表达式兼容,并替换使用
eval

import math

def main():
    print("\nTriangle Area Program\n")
    a, b, c = map(float, input("Enter three lengths separated by commas: ").split(','))

    if a + b > c and a + c > b and b + c > a:
        s = (a + b + c) / 2.0
        area = math.sqrt(s*(s-a)*(s-b)*(s-c))
        return round(area, 4) # round area to four decimal places
    else:
        raise ValueError("The inputs you entered cannot form a triangle")

if __name__ == '__main__':
    print(main())

更多关于避免
eval
的信息,如果可以

这里有另一种可能的数学问题:

import math


def heron(a, b, c):
    return 0.25 * math.sqrt((a + (b + c)) * (c - (a - b)) * (c + (a - b)) * (a + (b - c)))

if __name__ == "__main__":
    print()
    print("Triangle Area Program")
    print()
    print()

    try:
        description = "Enter three lengths separated by commas: "
        sides = sorted(map(float, input(description).split(',')))

        if (sides[1] + sides[2]) < sides[0]:
            print("A triangle cannot be formed.")
        else:
            a, b, c = sides
            print("Area of triangle {0}-{1}-{2} is {3:.4f}".format(
                sides[0], sides[1], sides[2], heron(a, b, c)))
    except Exception as e:
        print("Check your input!!!")
        print("--> Error: {0}".format(e))
导入数学
def heron(a、b、c):
返回0.25*math.sqrt((a+(b+c))*(c-(a-b))*(c+(a-b))*(a+(b-c)))
如果名称=“\uuuuu main\uuuuuuuu”:
打印()
打印(“三角形区域程序”)
打印()
打印()
尝试:
description=“输入以逗号分隔的三个长度:”
边=已排序(映射(浮点,输入(描述).split(','))
如果(边[1]+边[2])<边[0]:
打印(“无法形成三角形”)
其他:
a、 b,c=侧面
打印(“三角形{0}-{1}-{2}的面积为{3:.4f}”。格式(
边[0],边[1],边[2],苍鹭(a,b,c)))
例外情况除外,如e:
打印(“检查您的输入!!!”)
打印(“-->错误:{0}”。格式(e))
关于此版本的几个注意事项:

  • 它同时解析浮点输入值和排序,这样您就可以直接检查三角形是否可以形成
  • 它不是使用天真的heron公式,而是使用另一个

我决定给你另一个版本,因为在评论中你会发现一些关于你的好建议

使用
math.sqrt
math.sqrt
是你所需要的,而不是在那一行使用
eval
,你可以使用
a,b,c=map(float,input(“…”).split(“,”)
这就是应该发生的“三角形区域程序”输入三个长度,用逗号分隔:3、7、,9三角形的面积=8.7856平方单位。我的意思是三角形是不能形成的。如果b>c:,则只能附加到
。此外,您从未以任何形式输出您的计算面积这在我输入数字后不起作用,后面有一个空行这起作用,但在数字下它表示“无”,我如何消除这一点,如何将数字限制为4digits@MattPoretsky根据
None
,我感觉您有
打印(区域)
而不是
返回区域
:)我明白了,但是我需要它说“三角形的面积=,面积,平方单位”。你建议我怎么做。谢谢你可以用
打印
函数来做。将
打印(main())
替换为
打印(“三角形的面积=,main(),“平方单位”)
在输入numbers@MattPoretsky我添加了一些控制错误检查,您需要按照以下格式输入您的输入
a,b,c
,也就是说,一个字符串包含三个由commas@MattPoretsky这个版本已经在打印4位小数的区域了,看在
{3:.4f}