2015-09-10 59 views
1

這是我的程序到目前爲止,有人能告訴我我做了什麼錯誤或給我一個修復它嗎?這一切都在蟒蛇,我真的很感激答案。彈出錯誤消息說:「不能通過類型爲'str'的非整數來乘序列」。如何讓我的程序計算三角形的面積?在Python

height = input("What is the height of the triangle? ") 
width = input("What is the width of the triangle? ") 

area = width * height /2 

print("The area of the triangle would be {0:f} ".format(area)) 
+0

'嘗試轉換成int' – The6thSense

+0

你是什麼意思轉換爲int? – BlizzardGizzard

+0

見下面答案:) – The6thSense

回答

0

您應該將值轉換爲float數據類型:

height = float(input("What is the height of the triangle? ")) 
width = float(input("What is the width of the triangle? ")) 

area = width * height /2 

print("The area of the triangle would be {0:f} ".format(area)) 
+0

感謝Emil的回答。 :) – BlizzardGizzard

+0

這似乎工作,現在我已經嘗試過了。感謝所有的幫助。 – BlizzardGizzard

2

您正在使用input得到你的電話號碼,但input方法的返回類型爲字符串。將其轉換爲int並且它將起作用。

例子:

area = int(width) * int(height) /2 
+1

感謝您的幫助! – BlizzardGizzard

-2

實際上使用的是高=輸入(「什麼是三角形的高度?」)的輸入是不整,這裏是你可以解決什麼

height = eval(raw_input("What is the height of the triangle? ")) 
width = eval(raw_input("What is the width of the triangle?")) 
area = width * height /2 
print("The area of the triangle would be {0:f} ".format(area)) 
+0

raw_input是輸入的Python 2名稱,並且eval是危險的(正如在Python 2中輸入的那樣)。給定類型說明符:f,適當的類型將是float或decimal.Decimal。 –

+0

這是一種危險的做法。 'eval'將接受任何Python代碼。在這樣的玩具例子中,它可能工作得很好,但將其教導爲一般解決方案可能會導致輕鬆破解代碼,例如Web應用程序。 –

+1

猜測我不會使用該解決方案嗎? – BlizzardGizzard