我需要帮助,我的简单编程是使用NameError,我的代码:
class Televisao():
def __init__(self,boolean, channel):
self.channel = 2
self.boolean = False
def main():
tvhome =Televisao()
print tvhome.channel
if __name__== "__main__" :
main()
NameError:name' tvhome'未定义
答案 0 :(得分:2)
class Televisao:
def __init__(self, is_on, channel):
self.is_on = is_on
self.channel = channel
def main():
tvhome = Televisao(True, 13)
print(tvhome.channel)
if __name__== "__main__":
main()
答案 1 :(得分:0)
class Televisao():
def __init__(self,boolean, channel):
self.channel = 2
self.boolean = False
if __name__== "__main__" :
tvhome = Televisao()
print tvhome.channel
<强>更新强> 您似乎已在函数内部创建实例并从外部调用它。
def main():
tvhome = Televisao() # now tvhome is local variable.
print tvhome.channel # you will get the error : undefined
<强>更新强> 当您调用
时,将发生另一个错误tvhome = Televisao() # without its __init__ parameters boolean , channel