刚开始学习如何使用python,我不知道在哪里可以问我是否无法确定我的程序出了什么问题,如果这是错误的论坛那么有人可以请我指向正确的。它是一个计算器,设计用于在输入所有三个长度时给出三角形的三个角度。它似乎在编码方面起作用,但是当它们不应该时,所有3个角度都是相同的。最好解释一下,如果你运行下面的代码,你会看到我在说什么。同时计算三角形的类型是abit dodgy然而我确定这是我的错误,当仔细观察将解决自己。
MediaView
答案 0 :(得分: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