0

任务是写一个可以做三角形的代码(求周长,面积,如果是等边,右等)

我相信我的代码在目标上,但是当数字不形成三角形时,它不会像它应该呈现的那样呈现错误。任何帮助将不胜感激。

import math

A = int (input ("Type in your first integer that you would like to test:"))
print ("Your first integer to be tested is:", A)

B = int (input ("Type in your second integer that you would like to test:"))
print ("Your second integer to be tested is:", B)

C = int (input ("Type in your third integer that you would like to test:"))
print ("Your third integer to be tested is:", C)

if (A > B):
    largest = A
else:
    largest = B

if (C > largest):
    largest = C

print ("The largest number is:", largest)

if A<=0 or B<=0 or C<=0:
    print("The numbers don't form a triangle")
else:
    print("The Triangle's Perimeter is:")
    print(int(A+B+C))
    print("The semiperimeter is:")
    print(int((A+B+C)/2))
    print("The Area of the triangle is:")
    print (int (math.sqrt((A+B+C)/2)*(((A+B+C)/2)-A)*(((A+B+C)/2)-B)*(((A+B+C)/2)-C)))

if  int(A != B and A != C and B != C):
    print("The triangle is a scalene")
else:
    print ("The triangle is not a scalene")

if int(A == B and B == A or A == C and C == A or C == B and B == C):
    print ("The triangle is an isosceles")
else:
    print ("The triangle is not an isosceles")

if int(A == B == C):
    print("The triangle is an equilateral")
else:
    print("The triangle is not an equilateral")

if int(C*C == A*A + B*B):
    print("The triangle is a right triangle")
else:
    print("The triangle is not a right triangle")
4

1 回答 1

0

试试这个代码:

import math
def chkRectr(x1,x2,x3):
    if (x1**2==x2**2+x3**2)or(x2**2==x1**2+x3**2)or(x3**2==x2**2+x1**2):
        return True
    else:
        return False
def fHigh(aa,bb,cc):
    d=math.sqrt(3)/2
    if (aa==bb):
        return math.sqrt(aa**2-(cc/2)**2)
    elif (aa==cc):
        return math.sqrt(aa**2-(bb/2)**2)
    elif (bb==cc):
        return math.sqrt(bb**2-(aa/2)**2)

def trikind():
    if (a==b)or(b==c)or(a==c):
        if a==b==c:
            print("And it's kind: Equilateral triangle")
        else:
            if chkRectr(a,b,c)==True:
                print("And it's kind: Isosceles rectanglar triangle")
            else:
                print("And it's kind: Isosceles triangle")
            print("And its height is "+str(fHigh(a,b,c)))

    elif chkRectr(a,b,c)==True:
        print("And it's kind: Rectanglar triangle")

print("In this program I will tell you about existence of a triangle and kind of it.")

a,b,c=[float(x) for x in input("Enter three sides lenth(Split with ','): ").split(",")]


if a+b>c:
    if b+c>a:
        if c+a>b:
            print("\nIt's a triangle!\n")
            trikind()

else:
    print("\nSorry but It's not a triangle!!!! :((")

它与您的意思不同,但如果您注意到它擅长理解三角形!

于 2013-10-21T20:22:25.680 回答