NameError: name 'main' is not defined - indentation

NameError: name 'main' is not defined - indentation

我是 python 的新手,之前有人问过这个问题

  1. NameError: name '' is not defined
  2. NameError name 'Views' is not defined

但我的情况不同,这是我的程序

class student:    
    def address(self):
        print('address is mumbai')

    def contact(self):
        print('email : foo@yahoo.com')

    def main(self):
        _student=student()
        _student.address()
        _student.contact()    

if __name__ == "__main__":
    main()

我不知道是我的缩进导致了问题还是与方法的范围有关

main是classstudent内部的一个方法,所以你需要改变定义main的地方。

class student:    
    def address(self):
        print('address is mumbai')

    def contact(self):
        print('email : foo@yahoo.com')

def main():
    _student=student()
    _student.address()
    _student.contact()