當前位置:編程學習大全網 - 源碼下載 - 用Python怎麽用類的方法計算圓柱的表面積和體積,麻煩幫我寫出代碼,謝謝

用Python怎麽用類的方法計算圓柱的表面積和體積,麻煩幫我寫出代碼,謝謝

12345678910111213import mathclass cylinder():

def __init__(self,r,h):

self._r=r

self._h=h def volume(self):

return math.pi*self._r*self._r*self._h

def area(self): ?

return 2*math.pi*(self._r*self._r+self._r*self._h)r=float(input("請輸入圓柱的底面半徑"))h=float(input("請輸入圓柱的高"))

c=cylinder(r,h)print("圓柱的表面積是:%.1f\n 圓柱的體積是:%.1f"%(c.area(),c.volume()))

例如:

#! usr/bin/python

class Cube:

def __init__(self,l,w,h):

self.l = l

self.w = w

self.h = h

def surface(self):

result = (l*w+w*h+h*l)*2

print 'the surface of cube is '+str(result)

return result

def volume(self):

result = l*w*h

print 'the volume of cube is '+str(result)

return result

l = 2

w = 3

h = 4

a = Cube(l,w,h)

a.surface()

a.volume()

擴展資料:

根據PEP的規定,必須使用4個空格來表示每級縮進(不清楚4個空格的規定如何,在實際編寫中可以自定義空格數,但是要滿足每級縮進間空格數相等)。使用Tab字符和其它數目的空格雖然都可以編譯通過,但不符合編碼規範。支持Tab字符和其它數目的空格僅僅是為兼容很舊的的Python程序和某些有問題的編輯程序。

百度百科-Python

  • 上一篇:C語言程序設計,輸入壹個英文句子,統計單詞的個數。
  • 下一篇:哪個更“開放”:FreeBSD與Linux比較
  • copyright 2024編程學習大全網