2012-11-03 41 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))) 
相關問題