有谁知道 python class 吗?当我输入一些数据时,它的对象给我错误

does anyone know much about python class? and its objects is giving me errors when i input some data

if unit == 'F': self.number = (number - 32) * 5/9 UnboundLocalError:赋值前引用的局部变量'number'

number变量需要在to()函数的开头先声明。 在 to() 函数的第一行中输入一个简单的 : " number; " 即可解决问题。

class Temp:
    """A class to help work with lengths in various units"""
    number = 0
    def __init__(self, number, unit='C'):
        # Convert and store Temp as self.temp
        if unit == 'C': self.temp = number
        elif unit == 'F': self.temp = (number - 32) * 5/9
        elif unit == 'K': self.temp = number + 273.15
        # Unit not recognised
        else: raise Exception("Unit not recognised")
    
    def to(self, unit, dp=None):
        
        # Convert self.temp to unit
        if unit == 'C': number = (5/9 * self.temp ) + 32
        elif unit == 'C': number = self.temp - 273.15 
        
        else: raise Exception("Unit not recognised")
        if dp: number = round(number, dp)
        return f"{number}{unit}"
    
    def __str__(self):
        return f"Temp: {self.temp}C"
    
    def __add__(self, other):
        return Length(self.metres + other.metres)