Login() takes 1 positional argument but 2 were given

This error message “login() takes 1 positional argument but 2 were given” typically occurs when you are calling the login function with more arguments than it expects. In Python, functions are defined with a specific number of parameters, and when you call a function, you need to pass in the same number of arguments as the function definition.

Let’s say you have defined the login function like this:

    
def login(username):
    # Function code goes here
    # ...
    print("Welcome, " + username + "!")
    
  

As you can see, the login function expects only one parameter, which is the username. So, when you call the function, you should provide only one argument:

    
login("John")
    
  

If you mistakenly pass more than one argument, you will see the “login() takes 1 positional argument but 2 were given” error. For example:

    
login("John", "password")
    
  

In the above example, we are passing two arguments to the login function, but it expects only one. Hence, the error is raised.

Same cateogry post

Leave a comment