Skip to content
Advertisement

Rookie coder: main() missing 1 required positional argument: ‘self’?

This is my first coding class (and first time on the site) and I’m working on a class project. I can’t seem to get my code to run correctly. I’m getting 2 errors and I’ve spent the last 2 hours trying to figure them out. Was wondering if this site would be willing/able to help me out. Code in reply message.

Traceback (most recent call last):
  File "C:UsersArtPycharmProjectspythonProject1main.py", line 69, in <module>
    class Conversion:
  File "C:UsersArtPycharmProjectspythonProject1main.py", line 123, in Conversion
    main()
TypeError: main() missing 1 required positional argument: 'self'
class Conversion:
    def say_hello(self):
        print("Hello! I'm a Conversion:", self)

    def main(self):
        task = Conversion()
        task.say_hello()

    if __name__ == "__main__":
        main()

Note: The original code has been removed, but feel free to look at it in the edit history. This is an MRE provided by wjandrea.

Advertisement

Answer

It looks like you indented the last section wrong and added a self parameter to main for some reason (edit: apparently your IDE did that). Your code should look like this:

class Conversion:
    ...
    
def main():
    ...
   
if __name__ == "__main__":
    main()  # on a separate line only for readability
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement