求Python大神帮助解答 貌似要写程序 关于三角函数面积

PartC-AreaofaTriangleWriteaprogramthatpromptstheuserforthreesidedimensions,asfloats,w... Part C - Area of a Triangle
Write a program that prompts the user for three side dimensions, as
floats, which then outputs the area of a triangle made up from these
three sides. The side dimensions, a, b and c, must not only be greater
than zero, but they must satisfy what is called the "Triangle
Inequality
Theorem" which is a fancy name for the rule that states: "The sum of two
sides must add up to be greater than the length of the remaining third
side." In terms of the side lengths, these statements must be
satisfied:
a + b > c
b + c > a
a + c > b
The area of any triangle
can be calculated using what is commonly called "Heron's Rule" after Heron
of Alexandria, even though the theorem was first developed by Archimedes.
Given the three sides, a, b and c:
s = (a + b + c)/2
area = sqrt(s(s - a)(s - b)(s - c))
Use the math.sqrt() function to calculate the square root. To use this function in the math module, you will need to place the statment import math at the top of your program before your def main() : line. Limit the output decimal places to two digits after the decimal point.
展开
 我来答
浩星辰锟su
2013-01-30 · TA获得超过6765个赞
知道大有可为答主
回答量:975
采纳率:0%
帮助的人:459万
展开全部
程序已经帮您写好,下面是按照要求的程序,因为没有说明如果构不成三角形应该输出什么,所以暂时先输出This is an invalid triangle这样的字样,如果有明确的要求,可以更改。
import math
a,b,c=raw_input("Please enter length of three sides: ").split()
a=float(a)
b=float(b)
c=float(c)
if a<=0 or b<=0 or c<=0:
print "This is an invalid triangle."
elif a+b<=c or a+c<=b or b+c<=a:
print "This is an invalid triangle."
else:
s=(a+b+c)/2
area=math.sqrt(s*(s-a)*(s-b)*(s-c))
print "%.2f" % area
经过一系列测试,这个程序是完全可行的,用的是Python 2.7.3

若不懂,请追问,望采纳!
matlab2000
2013-01-30 · TA获得超过2323个赞
知道大有可为答主
回答量:1678
采纳率:100%
帮助的人:1085万
展开全部
import sys
import math
if len(sys.argv)<4:
print "you must input three side of a triangle"
sys.exit(-1)
try:
a,b,c=float(sys.argv[1]),float(sys.argv[2]),float(sys.argv[3])
if a+b>c and b+c>a and c+a>b and a>0 and b>0 and c>0:
s=(a+b+c)/2
area=math.sqrt(s*(s-a)*(s-b)*(s-c))
print "area is:",area
else:
print "the side can't match the rule"

except Exception as e:
print "we encount error:",e
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式