2012-11-03 39 views
-3
print "Welcome to Dylan's Pythagorean Theorem Solver." 

from math import sqrt 

print "What are we solving for? A hypotenuse or a leg?" 

ask = raw_input("# ") 

if ask == "hypotenuse": 
    print "What is the value of your first leg?" 

    leg1 = raw_input("# ") 

    print "The value of your first leg is %s. What is the value of your second leg?" % (leg1) 

    leg2 = raw_input("# ")  

    print "The length of your hypotenuse is "sqrt((leg1 ** 2) + (leg2 ** 2)) 
+3

当您尝试运行它时会发生什么? – xpda

+0

你应该注意你收到了什么错误信息。 –

回答

2

我是个初学者,但我这是怎么了你的代码工作:

产生的raw_input字符串。您需要将leg1和leg2转换为整数或浮点数,然后才能在sqrt中使用它们。你可以是这样做的:

leg1 = int(input("# ")) 

你有同样的问题,但在打印反向(Python是期待一个海峡,但得到的浮动)。您还错过了打印中的操作员。

只需为sqrt的结果创建一个新变量,将其转换为str,然后在print中使用该变量就可能更容易。

hypotenuse = str(sqrt((leg1 ** 2) + (leg2 ** 2))) 
相关问题