0
点赞
收藏
分享

微信扫一扫

几个python 编程题。

落拓尘嚣 2023-11-29 阅读 36
'''
请定义一个Cylinder类,
(1)每个Cylinder类对象可以存储一个圆柱体(包括半径和高)
(2)具有用于初始化半径和高的方法
(3)具有输出圆柱体信息的方法Printlnfo ;
(4)具有计算圆柱体体积的方法GetVolume.
'''


class Cylinder:

    def __init__(self, radius=0.0, height=0.0):
        self.r = radius
        self.h = height

    def Printlnfo(self):
        print("半径:"+str(self.r)+",高:"+str(self.h))

    def GetVolume(self):
        return 3.14*(self.r**2)*self.h


c = Cylinder(3.5,7.2)
c.Printlnfo()
print("圆柱体体积:"+str(c.GetVolume()))





class Circle:
    def __init__(self, r=0.0):
        self.__r = r
    def setR(self,r):
        self.__r = r
    def Area(self):
        return 3.14 * (self.__r**2)

    def __str__(self):
        return "面积是:"+str(self.Area())

    def __gt__(self,other):
        return self.Area() > other.Area()

c1 = Circle(4.0)
c2 = Circle(4.0)
print(c1)
print(c2)
print(c1>c2)
c1.setR(5)
print(c1>c2)


class Father:
    def do(self):
        print("会弹奏钢琴")


class Son1(Father):
    def do(self):
        super().do()
        print()

class Son2(Father):
    def do(self):
        print("擅长弹小提琴")
        print()

class Son3(Father):
    def do(self):
        super().do()
        print("会弹奏小提琴")
        print()

father = Father()
son1 = Son1()
son2 = Son2()
son3 = Son3()

father.do()
son1.do()
son2.do()
son3.do()


# 方法1
def f(m, n):
    global s
    if m % n != 0:
        s += n
    else:
        s = s



m = eval(input("请输入一个整数m:"))
s = 0
while True:
    n = eval(input("请输入一个整数n:"))
    if n == 0:
        break
    if n != 0:
        f(m, n)
print("不能被%d整除的数字之和为%d" % (m, s))
举报

相关推荐

0 条评论