python3.4勾股定理代码_使用有序数组来解决勾股定理(Python)
好吧,在畢達哥拉斯定理中,您需要斜邊是最長的長度。我能夠對我的數組進行排序,它會自動將我的值從最小到最大排列。現在,問題是我該如何處理這些值?程序中的用戶不知道什么是最長的長度(用戶輸入坐標),計算機繪制三個點并使用距離公式來查看三面之間的長度。使用有序數組來解決勾股定理(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]'? –
總結
以上是生活随笔為你收集整理的python3.4勾股定理代码_使用有序数组来解决勾股定理(Python)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一个朋友的精彩BLOG
- 下一篇: Mac搜不到wifi