SyntaxError: non-default argument follows default argument

SyntaxError: non-default argument follows default argument

If you are getting trouble with the “SyntaxError: non-default argument follows default argument”, keep reading our article. We will give you some methods to handle the problem.

Reason for “SyntaxError: non-default argument follows default argument”

SyntaxError is one of the most common errors in the Python programming language that occurs when you use an incorrect syntax. For example, in the C programming language, you can increase an index i by syntax i++, but you will receive the error when using it like this in Python.

You will get the SyntaxError: non-default argument follows default argument when you create a function that contains any parameters with initial value standing in front of non-default augment. Take a look at the following example. 

Code:

import math
 
# ax2 + bx + c = 0
def quadratic(a,b=3,c):
  if a==0:
    return -c/b
  delta = b**2 - 4*a*c
  if delta < 0:
    print("The equation has no solution")
    return 
  elif delta == 0:
    print("The equation has only one solution:")
    return -b/(2*a)
  else:
    print("The equation has two different solutions:")
    return (-b+math.sqrt(delta))/(2*a), (-b-math.sqrt(delta))/(2*a)

Result:

line 4
    def quadratic(a,b=3,c):
                 ^
SyntaxError: non-default argument follows default argument

Now you understand the problem clearly. Let’s move on to discover how to solve the problem.

Solution for this error

Remove initial value

In the Python programming language, it is easy to pass any value to the parameters of a function. So, you do not need to define the initial value for the parameter if it is not in particular situations. To fix this problem, you only need to create parameters without value. 

We will represent the solution by the following code;

Code:

import math
 
# ax2 + bx + c = 0
def quadratic(a,b,c):
  if a==0:
    return -c/b
  delta = b**2 - 4*a*c
  if delta < 0:
    print("The equation has no solution")
    return 
  elif delta == 0:
    print("The equation has only one solution:")
    return -b/(2*a)
  else:
    print("The equation has two different solutions:")
    return (-b+math.sqrt(delta))/(2*a), (-b-math.sqrt(delta))/(2*a)
  
solution = quadratic(5,3,-8)
print(solution)

Result:

The equation has two different solutions:
(1.0, -1.6)

Change the order of parameters

Syntax of the programming language prevents you from creating a parameter with an initial value before one that is not declared the initial value. Read here for more detail.

If you still want to define the value for the parameter when creating the function, put parameters having the initial value in the final to avoid the error. 

Code:

import math
 
# ax2 + bx + c = 0
def quadratic(a,b,c=-8):
  if a==0:
    return -c/b
  delta = b**2 - 4*a*c
  if delta < 0:
    print("The equation has no solution")
    return 
  elif delta == 0:
    print("The equation has only one solution:")
    return -b/(2*a)
  else:
    print("The equation has two different solutions:")
    return (-b+math.sqrt(delta))/(2*a), (-b-math.sqrt(delta))/(2*a)
  
solution = quadratic(5,3)
print(solution)

Result:

The equation has two different solutions:
(1.0, -1.6)

In this example, the parameter c has declared the value when creating the function. However, the parameter’s position is the last, so there is no error.

Summary

Our article has explained the “SyntaxError: non-default argument follows default argument”. We also give you some ways to fix the error. If you have any questions, please give your comments below. I will answer as possible. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *