Python编程基础 第四章 编程练习 请定义一个Cylinder类,具体要求为:(1)每个Cylinder类对象可以存储一个圆柱体;2)具有用于初始化半径和高的构造方法;(3)具有计算圆柱体体积

题目内容:

请定义一个Cylinder类,具体要求为:(1)每个Cylinder类对象可以存储一个圆柱体(包括半径和高);(2)具有用于初始化半径和高的构造方法;(3)具有计算圆柱体体积的方法GetVolume。

import math
#请在此处写出Cylinder类定义的代码(提示:计算体积时使用math.pi作为圆周率)

if __name__=='__main__':
    r=eval(input()) #输入半径
    h=eval(input()) #输入高
    c=Cylinder(r,h) #创建Cylinder对象
    print('radius:%.2f,height:%.2f'%(c.r,c.h)) #输出半径和高
    print('volume:%.2f'%c.GetVolume()) #输出体积

输入格式:

分两行分别输入半径和高

输出格式:

第一行输出半径和高,第二行输出体积

输入样例:

3
5.2

输出样例:

radius:3.00,height:5.20
volume:147.03

时间限制:500ms内存限制:32000kb

代码:

import math

class Cylinder:
    def __init__(self, radius, height):
       self.r = radius
       self.h = height
       
    def GetVolume(self):
        return math.pi * self.r**2 * self.h

if __name__=='__main__':
    r=eval(input()) #输入半径
    h=eval(input()) #输入高
    c=Cylinder(r,h) #创建Cylinder对象
    print('radius:%.2f,height:%.2f'%(c.r,c.h)) #输出半径和高
    print('volume:%.2f'%c.GetVolume()) #输出体积
用例测试结果运行时间占用内存提示得分
用例1通过30ms4624kb1
用例2通过32ms4516kb1

提交答案

本次得分/总分:2.00/2.00分

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值