2017-11-16 7 views
-3

방금 ​​파이썬을 사용하는 법을 배우기 시작했으며, 내 프로그램에 어떤 문제가 있는지 확인할 수 없는지 묻습니다. 잘못된 포럼 일 경우 직접 알려주십시오. 나에게 맞는 것. 3 개의 길이가 모두 입력 될 때 삼각형의 3 개의 각을 제공하도록 설계된 계산기입니다. 그것은 코딩의 측면에서 작동하는 것 같지만 3 가지 각도는 동일하지 않아야합니다. 아래 코드를 실행하면 내가 말하는 것을 볼 수 있습니다. 또한 삼각형의 유형을 해결 abit dodgy하지만 이것은 내 자신의 문제를 면밀히 검토 할 때 내 자신의 문제를 해결할 것이라고 확신합니다.수식 오류를 확인할 수 없습니다

import math 

#degrees = (180/pi).radians (python maths works in radians) & radians = (pi/180).degrees 

print("Welcome to the tri-angle calculator, please ensure that all 3 length inputs form a triangle otherwise program will not function. They must also be converted to the same units of length") 

a = int(input("Insert first length of triangle in degrees: ")) 
b = int(input("Insert second length of triangle in degrees: ")) 
c = int(input("Insert third length of triangle in degrees: ")) 
#length inputs 

A = math.acos(((b ** 2) + (c ** 2) - (a ** 2))/(2 * b * c)) 
B = math.acos(((a ** 2) + (c ** 2) - (b ** 2))/(2 * a * c)) 
C = math.acos(((a ** 2) + (b ** 2) - (c ** 2))/(2 * a * b)) 
#working out all 3 angles of triangles 

if(a == c and b == c): 
    type = "Equilateral Triangle" 
elif(math.cos(A) == 0 or math.cos(B) == 0 or math.cos(C) == 0): 
    type = "Right Angle Triangle" 
elif((a == b and c != b) or (b == c and c == a) or (c == a and c != b)): 
    type = "Isoceles Triangle" 
elif(a != b and b != c and c != a and A < (0.5 * math.pi) and B < (0.5 * math.pi) and C < (0.5 * math.pi)): 
    type = "Acute Triangle" 
else: 
    type = "Obtuse Triangle" 
#Working out the triangle type 

print("The order of which the angles are shown in are opposite to the length inputted:") 
print("Angle A - " + str((180/math.pi) * A)) 
print("Angle B - " + str((180/math.pi) * A)) 
print("Angle C - " + str((180/math.pi) * A)) 

print("And it's a " + type) 
#Printing Results 

exit() 
+1

봐 (마지막 5 줄) – user3080953

+0

참고 :'a','b','c' 숫자가 실제로 삼각형이 될 수 있는지 확인하지 않습니다. 또한 길이는도 단위로 측정되지 않습니다. 하하하, 좋은 사람 @ user3080953! – CristiFati

+3

코드 블록이 그 블록이 아닌 블록 뒤에 무엇을하는지에 대한 주석을 넣는 것은 매우 혼란 스럽습니다. – Barmar

답변

1

이 프로그램은 고정 및 기능 (반드시 최고의 하나,하지만 당신에게 아이디어를 제공 할 수 있습니다 :))

import math 

#degrees = (180/pi).radians (python maths works in radians) & radians = (pi/180).degrees 

print("Welcome to the tri-angle calculator, please ensure that all 3 length inputs form a triangle otherwise program will not function. They must also be converted to the same units of length") 


def magic_in_progress(a, b, c): 
    A = math.acos((b**2 + c**2 - a**2)/(2 * b * c)) 
    B = math.acos((a**2 + c**2 - b**2)/(2 * a * c)) 
    C = math.acos((a**2 + b**2 - c**2)/(2 * a * b)) 
    #working out all 3 angles of triangles 

    name = "Undetermined Triangle" 

    pi_over_2 = 0.5 * math.pi 

    if a == b == c: 
     name = "Equilateral Triangle" 
    elif 0 in (math.cos(A), math.cos(B), math.cos(C)): 
     name = "Right Angle Triangle" 
    elif a == b != c or b == c != a or c == a != b: 
     name = "Isoceles Triangle" 
    elif a != b != c != a and A < pi_over_2 and B < pi_over_2 and C < pi_over_2: 
     name = "Acute Triangle" 
    else: 
     name = "Obtuse Triangle" 
    #Working out the triangle type 

    print("The order of which the angles are shown in are opposite to the length inputted:") 
    print(f"Angle A - {180/math.pi * A}") 
    print(f"Angle B - {180/math.pi * B}") 
    print(f"Angle C - {180/math.pi * C}") 

    return name 

#length inputs 
a = int(input("Insert first length of triangle: ")) 
b = int(input("Insert second length of triangle: ")) 
c = int(input("Insert third length of triangle: ")) 

triangle_type = magic_in_progress(a, b, c) 

print(f"And it's a {triangle_type}") 

그리고 경우에 사용 :

>>> a = int(input("Insert first length of triangle: ")) 
Insert first length of triangle: 4 
>>> b = int(input("Insert second length of triangle: ")) 
Insert second length of triangle: 5 
>>> c = int(input("Insert third length of triangle: ")) 
Insert third length of triangle: 6 
>>> triangle_type = magic_in_progress(a, b, c) 
The order of which the angles are shown in are opposite to the length inputted: 
Angle A - 41.40962210927086 
Angle B - 55.771133672187425 
Angle C - 82.81924421854173 
>>> print(f"And it's a {triangle_type}") 
And it's a Acute Triangle 
+0

사실, 수식을 확인하지 않았습니다. –

+0

@ PM2Ring 방금 그들을 고치고 그들을 더 평범한 사람으로 만들도록 청소했습니다. 나중에 읽을 수있는 사람들에게 유용 할 수 있습니다. –