我需要一个帮助我的简单编程与NameError

时间:2014-03-13 23:33:34

标签: python class nameerror

我需要帮助,我的简单编程是使用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'未定义

2 个答案:

答案 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