class area:
def traingle(self,height,length):
self.height=height
self.length=length
def calculate(self,maths):
self.maths= (self.height)*(self.length)*(0.5)
def answer(self):
print 'Hello, the aswer is %i'%self.maths
first= area()
first.traingle(4,5)
first.calculate
print first.answer
1
A
回答
2
這個什麼領域?
import math
class Triangle:
def __init__(self, height, length):
self.height = height
self.length = length
def calculate(self):
return (self.height) * (self.length) * (0.5)
def answer(self):
print 'Hello, the aswer is %.2f' % self.calculate()
first = Triangle(4, 5)
first.answer()
記住,要叫你需要使用括號的方法,當你做first.answer
你不執行你的方法,而不是你應該做first.answer()
這種類型的另一種不同的解決方案問題可能是這樣的:
import math
class Triangle:
def __init__(self, height, length):
self.height = height
self.length = length
def area(self):
return (self.height) * (self.length) * (0.5)
class Quad:
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
for index, obj in enumerate([Triangle(4, 5), Quad(2, 3)]):
print 'Area for {0} {1} = {2:.2f}'.format(obj.__class__, index, obj.area())
在任何情況下,請務必通過一些可用的python tutorials在那裏,以先了解所有的概念;-)
相關問題
- 1. 在我的VHDL代碼錯誤,但我似乎無法弄清楚爲什麼
- 2. 我似乎無法弄清楚我做錯了什麼?
- 3. 東西我的代碼,我似乎無法弄清楚
- 4. 我無法弄清楚什麼是錯在我更新代碼
- 5. 我無法弄清楚什麼是錯
- 6. 無法弄清楚什麼是錯我的JavaScript代碼
- 7. 我似乎無法弄清楚爲什麼這個查詢給我錯誤
- 8. 我無法弄清楚我的Java代碼有什麼問題
- 9. 問題與我的CSS,我似乎無法弄清楚
- 10. 似乎無法弄清楚爲什麼我的JavaScript不工作在IE和Chrome
- 11. 無法弄清楚我的代碼有什麼問題
- 12. 無法弄清楚我的PHP代碼有什麼問題
- 13. NullPointerException在我的代碼,但無法弄清楚爲什麼
- 14. 無法弄清楚我的Javascript代碼中有什麼問題
- 15. 無法弄清楚什麼是錯我的CSS
- 16. 我似乎無法弄清楚如何調試我的TFS 2010生成
- 17. 似乎無法弄清楚爲什麼InnerHTML語句不工作
- 18. ,我無法弄清楚
- 19. 我無法弄清楚OAuth2
- 20. 的錯誤,我無法弄清楚
- 21. 我無法弄清楚的sql錯誤
- 22. 我需要弄清楚我的代碼
- 23. MySQL的語法錯誤,似乎無法弄清楚
- 24. 無法弄清楚什麼是錯的。如何調試
- 25. 爲什麼下面的C代碼不工作?我無法弄清楚錯誤。
- 26. 我無法弄清楚我的Python書要求我做什麼?
- 27. 我的代碼返回幾個空值,我無法弄清楚爲什麼
- 28. 我無法弄清楚我的表單代碼有什麼問題
- 29. 我無法弄清楚我的html代碼出了什麼問題
- 30. fileIn,fileOut錯誤 - 我無法弄清楚
那麼你的代碼有什麼問題?你有錯誤嗎?你會得到意想不到的結果嗎?它是什麼? – Matthias
<方法classarea.answer <__ main __。classarea instance at 0x101978a28 >> –
missing __init__ – Mixone