好吧,在毕达哥拉斯定理中,您需要斜边是最长的长度。我能够对我的数组进行排序,它会自动将我的值从最小到最大排列。现在,问题是我该如何处理这些值?程序中的用户不知道什么是最长的长度(用户输入坐标),计算机绘制三个点并使用距离公式来查看三面之间的长度。使用有序数组来解决勾股定理(Python)
下面是可以对值进行排序的代码。
#result3, result2, result are leg measurements
brandonarray[result3,result2,result]
brandonarray.sort(key=int)
因此,举例来说,如果用户积3个随机点(允许使用(3,5),(10,10),(19,20)) 我提出的程序打印阵列距离数。
#result3 was 8, result2 was 13, result was 21
[8,13,21]
如果我插入这三个数字,我知道它会工作。我做了这个程序,告诉你这些腿的尺寸会让它变成斜角,它是一个钝角三角形。
但是如果用户在不注意边长的情况下提出了要点呢?
#all of these are strings b/c in the beginning of program, user has input
joe=str(float(result3)**int(2))
sally=str(float(result2)**int(2))
print(str(float(result3)**int(2)),"+",float(result2)**int(2),"=",float(result)**int(2))
print("")
print(str(float(joe)+float(sally)),"=",str(float(result)**int(2)))
#this code no matter what, will take the number its assigned too, regardless if its bigger than the hypotenuse
if(str(float(joe)+float(sally))>str(float(result)**int(2))):
print("This is an acute triangle!")
elif(str(float(joe)+float(sally))
print("This is an obtuse triangle!")
elif(str(float(joe)+float(sally))==str(float(result)**int(2))):
print("This is an right triangle!")
什么我真的问的是我怎么可以使用数组由上述可见,并落实到勾股定理,从最小的值最大。非常感谢您的帮助!
2017-03-07
PYRO 912
+1
转码过多浮点数 int str在您的代码中。你应该能够删除其中的大部分。 –
+1
特别是,'int(2)'是一个no-op。 –
+2
'brandonarray [result3,result2,result]'?你的意思是'brandonarray = [result3,result2,result]'? –