python计算圆柱体积 | 您所在的位置:网站首页 › 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("圆柱的表面积32313133353236313431303231363533e59b9ee7ad9431333433653936是:%.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程序和某些有问题的编辑程序。 |
CopyRight 2018-2019 实验室设备网 版权所有 |