输入三角形边的长度和输出三角形的类型。不起作用

时间:2018-09-02 18:17:59

标签: python

所以,这是我的代码,而python似乎没有以粗体显示这些行。基本上,这是一个代码,用户输入三个三角形边的三个长度,并输出三角形的类型(等边,等腰,斜角(不起作用。请帮帮我)或对)。

    x= input (" Length of side 1 =  ")
    y= input ("Length of side 2 =  ")
    z= input ("Length of side 3 = ")

    x= float (x)
    y= float (y)
    z= float (z)

    flag=0

    if x==y==z:
      print ("This is an equilateral triangle")
    flag=1

    if x>y and x>z and (x**2) == ((z**2) + (y**2)):
      print ("This is a right triangle")
    flag=1

    if z>y and z>x and (z**2) ==((x**2) + (y**2)):
     print ("This is a right triangle")
    flag=1

    if y>z and y>x and (y**2) == ((z**2) + (x**2)):
     print ("This is a right triangle")
    flag=1

    **if x!=z!=y and flag==0:
     print("This triangle is scalene")**

    if x==z and x!= y:
     print ("This triangle is isosceles")

    if x==y and x!= z:
     print ("This triangle is isosceles")

    if z==y and z!= x:
     print ("This triangle is isosceles")

1 个答案:

答案 0 :(得分:0)

一些小的改进可以帮助您学习:

def identify_triangle(x, y, z):
    def tri_print(tri_type):
        print("This is an {} triangle".format(tri_type))

    if all([x==y, x==z]):
        triangle_type = "equilateral"

    elif any([
        all([x>y, x>z, (x**2)==((z**2)+(y**2))]),
        all([z>y, z>x, (z**2)==((x**2)+(y**2))]),
        all([y>z, y>x, (y**2)==((z**2)+(x**2))]),
        ]):
        triangle_type = "right"

    elif all([x!=z, z!=y, x!=y]):
        triangle_type = "scalene"

    elif any([
        all([x==z, x!=y]),
        all([x==y, x!=z]),
        all([z==y, z!=x]),
    ]):
        triangle_type = "isosceles"
    else:
        triangle_type = "Unknown"

    tri_print(triangle_type)
    return triangle_type

x= float(input("Length of side 1 = "))
y= float(input("Length of side 2 = "))
z= float(input("Length of side 3 = "))
print()
identify_triangle(x, y, z)