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]'? –

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值