python逻辑练习

文章介绍了如何使用Python类来实现华氏度、摄氏度和开氏度之间的转换,并提供了计算三角形、长方形和圆形面积的方法。通过定义不同的类,如Triangle1、Rectangle1和Circle1,实现了基于几何属性计算面积的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

练习

1.华氏度,摄氏度,开氏度转换

转换公式:

℃ = 5 × (℉ - 32) / 9

℉ = 9 × ℃ / 5 + 32

K = ℃ + 273.15

可能比较难理解的就是

开氏度到华氏度的相互转换,但是理解意思后也就那么回事

这里我单独拿出来举例

#此代码是下方类中的开氏度到华氏度的相互转换
    @classmethod
    def f2k(cls,f):  #套公式,华氏度--->开氏度 
        return  cls.c2k(cls.f2c(f))  #首先要讲华氏度变为摄氏度,然后根据公式K = ℃ + 273.15,反推

    @classmethod
    def k2f(cls,k): #同理套公式,开氏度--->华氏度 
        return  cls.c2f(cls.k2c(k)) #将开氏度-转变为摄氏度,然后根据公式K = ℃ + 273.15,反推

class Temperature_test1:

    def __int__(self,c,f):
        self.__int__()


    @classmethod
    def c2f(cls,c):
        return 9 * c / 5 + 32

    @classmethod
    def f2c(cls,f):
        return (f - 32) * 5 / 9

    @classmethod
    def  c2k(cls,c):
        return  c + 273.15

    @classmethod

    def k2c(cls,k):
        return  k - 273.15



# print (Temperature_test1.c2f(20))
# print (Temperature_test1.f2c(68))
# print (Temperature_test1.c2k(100))
# print (Temperature_test1.k2c(373.15))
# #
# k to f
#
# f to k

# ℃ = 5 × (℉ - 32) / 9
# ℉ = 9 × ℃ / 5 + 32
# K = ℃ + 273.15


class  Temperature_test2:

    @classmethod
    def c2f(cls,c):
        return  9 * c / 5 +32

    @classmethod
    def f2c(cls,f):
        return  (f - 32 ) * 5 /9

    @classmethod
    def c2k(cls,c):
        return  c + 273.15

    @classmethod
    def k2c(cls,k):
        return  k - 273.15

    @classmethod
    def f2k(cls,f):
        return  cls.c2k(cls.f2c(f))

    @classmethod
    def k2f(cls,k):
        return  cls.c2f(cls.k2c(k))


# print (Temperature_test1.c2f(100),"f")
# print (Temperature_test1.f2c(212),"C")
# print (Temperature_test1.c2k(212),"K")
# print (Temperature_test1.k2c(485.15),"c")
# print (Temperature_test1.f2k(100))
# print (Temperature_test1.k2f(310.92777777777775))
# ℃ = 5 × (℉ - 32) / 9
# ℉ = 9 × ℃ / 5 + 32
# K = ℃ + 273.15

class Temperature_test3:

    def __int__(self,t,unit='c'):
        self._c = None
        self._f = None
        self._k = None
        if unit == 'f' :
            self._f = t
            self._c = self.f2c(t)
        elif unit == 'k':
            self._k = t
            self._c = self.k2c(t)
        else:
            self._c = t


    @classmethod
    def f2c(cls,f):
        return   (f - 32) * 5 /9

    @classmethod
    def c2f(cls,c):
        return  9 * c / 5 +32

    @classmethod
    def c2k(cls,c):
        return  c + 273.15

    @classmethod
    def k2c(cls,k):
        return k - 273.15

    @classmethod 
    def f2k(cls,f):
        return cls.c2k(cls.f2c(f))
    @classmethod
    def k2f(cls,k):
        return cls.c2f(cls.k2c(k))


# print (Temperature_test3.f2c(100))
# print (Temperature_test3.c2f(37.77777777777778))
# print (Temperature_test3.k2c(100))
# print (Temperature_test3.c2k(-173.14999999999998))
# print (Temperature_test3.f2k(100))
# print (Temperature_test3.k2f(310.92777777777775))



2.计算三角形,长方形,园的面积

面积公式:

三角形: (长+宽+高)/2 如果不知道的话去百度

长方形:长 * 宽

圆形:3.14 * d * d * 0.25 ,d = 直径

下面就是逻辑了,懒得重新理了,大家自己理吧

 
import  math


class shape1: #
    @property
    def area(self):
        raise  NotImplementedError("基类未实现")



class Triangle1(shape1): #三角形
    def __init__(self,a,b,c):
        self.a = a
        self.b = b
        self.c = c
    @property
    def area(self):
        p  = (self.a + self.b + self.c ) / 2
        return  math.sqrt(p * (p - self.a) * (p - self.b) * (p - self.c) )




class Rectangle1(shape1): #长方形
    def __init__(self,width,height):
        self.width = width
        self.heigth = height

    @property
    def area(self):
        return  self.width * self.heigth


class Circle1(shape1): #圆形
    def __init__(self,redius):
        self.d = redius  * 2

    @property
    def area(self):
        return  math.pi * self.d * self.d * 0.5


shape =  [ Triangle1(3,4,5),Rectangle1(10,20),Circle1(4)]

# for i in shape:
#     print ('the are of {} = {}'.format(i.__class__.__name__,i.area))


--------------------------------------------------------------------------------------------------------------------------------

class shape2 :
    def __init__(self):
        self._area = None

    def area(self):
        raise  NotImplementedError('基类未实现')


class Triangle2(shape2):
    def __init__(self,a,b,c):
        super().__init__()
        self.a = a
        self.b = b
        self.c = c
        self.p = (self.a + self.b + self.c ) / 2
    @property
    def area(self):
        if self._area  is None :
            p = self.p
            self._a = math.sqrt(p * (p - self.a) * (p - self.b) * (p - self.c))
        return  self._area


class Rectangle2(shape2):

    def __init__(self,width,height):
        super().__init__()
        self.width = width
        self.heigth = height

    def area(self):
        if self._area is None:
            self.area = self.width * self.heigth
        return  self._area



class Circle2(shape2):

    def __init__(self,radius):
        super().__init__()
        self.redius = radius
        self.d = self.redius * 2

    def area(self):
        if  self._area is None:
            self.area  = 3.14 * self.d * self.d * 0.25
        return  self._area

shape2  = [ Triangle2(3,4,5) , Rectangle2(3,4),Circle2(3)]
for i in shape :
    print ("{} {} ".format(i.__class__.__name__,i.area))


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值