python问题,我的function不能return,讲的是一个开关灯的类class LightSwitch:def __init__(self,condition):self.is_on = conditionif not isinstance(condition,bool):InvaildSwitchExpection = TypeError("it is not a right state".format(condition
来源:学生作业帮助网 编辑:作业帮 时间:2024/06/18 20:04:10
![python问题,我的function不能return,讲的是一个开关灯的类class LightSwitch:def __init__(self,condition):self.is_on = conditionif not isinstance(condition,bool):InvaildSwitchExpection = TypeError(](/uploads/image/z/12462096-48-6.jpg?t=python%E9%97%AE%E9%A2%98%2C%E6%88%91%E7%9A%84function%E4%B8%8D%E8%83%BDreturn%2C%E8%AE%B2%E7%9A%84%E6%98%AF%E4%B8%80%E4%B8%AA%E5%BC%80%E5%85%B3%E7%81%AF%E7%9A%84%E7%B1%BBclass+LightSwitch%3Adef+__init__%28self%2Ccondition%29%3Aself.is_on+%3D+conditionif+not+isinstance%28condition%2Cbool%29%3AInvaildSwitchExpection+%3D+TypeError%28%22it+is+not+a+right+state%22.format%28condition)
python问题,我的function不能return,讲的是一个开关灯的类class LightSwitch:def __init__(self,condition):self.is_on = conditionif not isinstance(condition,bool):InvaildSwitchExpection = TypeError("it is not a right state".format(condition
python问题,我的function不能return,讲的是一个开关灯的类
class LightSwitch:
def __init__(self,condition):
self.is_on = condition
if not isinstance(condition,bool):
InvaildSwitchExpection = TypeError("it is not a right state".format(condition))
raise InvaildSwitchExpection
def turn_on(self):
self.is_on = True
def turn_off(self):
self.is_on = False
def filp(self):
if self.is_on == False:
self.is_on = True
else:
self.is_on = False
def __str__(self):
if self.is_on == True:
return "I am on"
else:
return "I am off"
这是第一个类,下面还有一个,那个which_switch不能return,return出来什么都没有
python问题,我的function不能return,讲的是一个开关灯的类class LightSwitch:def __init__(self,condition):self.is_on = conditionif not isinstance(condition,bool):InvaildSwitchExpection = TypeError("it is not a right state".format(condition
类里面定义的不是函数,而是方法.我没见过方法可以“return”.