2014-03-13 42 views
0

HI我需要幫助,我的簡單編程是NameError,我的代碼:HI我需要幫助,我的簡單編程是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' is not defined

+0

請檢查你的代碼格式,並提供完整的錯誤回溯。但請注意,「Televisao」!=「televisao」'。 – jonrsharpe

+0

看起來像你的意思是'tvhome = Televisao()'而不是'televisao()'沒有大寫't'。因此這似乎是無關緊要的,因爲它是一個錯字。 –

回答

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() 
0
class Televisao(): 
    def __init__(self,boolean, channel): 
     self.channel = 2 
     self.boolean = False 

if __name__== "__main__" : 
    tvhome = Televisao() 
    print tvhome.channel 

UPDATE: 看起來你已經在函數內創建了實例並從外部調用它。

def main(): 
    tvhome = Televisao() # now tvhome is local variable. 

print tvhome.channel # you will get the error : undefined 

更新: 另一個errror會發生,因爲你叫

tvhome = Televisao() # without its __init__ parameters boolean , channel