I have one function that calculates the difference between two numbers but I am facing the following error with this function: TypeError: calculate_diff() missing 2 required positional arguments: ‘num1’ and ‘num2’ in Python.We are going to Learn about All Possible Solutions So Lets Get Start with This Article.
How TypeError: missing 2 required positional arguments Error Occurs?
I have one function that calculates the difference between two numbers but I am facing the following error with this function. Here is my code.
def calculate_diff(num1, num2):
return num1-num2
output = calculate_diff()
print("Diff Between Two Number is: ", output)
Error That I am facing.
TypeError: calculate_diff() missing 2 required positional arguments: 'num1' and 'num2'
So here I am writing all the possible solutions that I have tried to resolve this error.
How To Solve TypeError: missing 2 required positional arguments?
- How To Solve TypeError: missing 2 required positional arguments?
To Solve TypeError: missing 2 required positional arguments Just Giving Function to the Default Value. Default Value will act Like If You Give them value then it will Perform on that value else it will take Its Default Value. In the above code, I have the calculate_diff function and that required two positional parameters num1 and num2. But I have Given Default values to both num1=0 and num2=0. So when I am using my calculate_diff function and if I am not giving value to this function then it will take this default value. and Give me output just like below: Diff Between Two Number is: 0 And Now, Your error must be solved.
- TypeError: missing 2 required positional arguments
To Solve TypeError: missing 2 required positional arguments This error Usually Occurs when You are not passing Positional Parameters to the Function. So easiest ever method to solve this error Is Just to Pass the Required Positional Parameters to the Function. Here is My Function named calculate_diff and that is required two positional Parameters num1 and num2. When I am trying to use calculate_diff I have passed both parameters as 10 and 9. So that My function gets its Positional Parameters and it will return me Calculated Difference. Just like Below Output. And My Problem was Resolved Successfully.
There are Two Main Methods to solve this error. The first method is Just to Pass the Required Parameters to the function and the Second one is You need to Assing the Default Value of the Function. Let’s see both Solutions one by one.
Solution 1: Pass required Params
This error Usually Occurs when You are not passing Positional Parameters to the Function. So easiest ever method to solve this error Is Just to Pass the Required Positional Parameters to the Function.
def calculate_diff(num1, num2):
return num1-num2
output = calculate_diff(10, 9)
print("Diff Between Two Number is: ", output)
Here is My Function named calculate_diff and that is required two positional Parameters num1 and num2. When I am trying to use calculate_diff I have passed both parameters as 10 and 9. So that My function gets its Positional Parameters and it will return me Calculated Difference. Just like Below Output.
Diff Between Two Number is: 1
And My Problem was Resolved Successfully.
Solution 2: Give Default Value
The second Solution is Just Giving Function to the Default Value. Default Value will act Like If You Give them value then it will Perform on that value else it will take Its Default Value.
def calculate_diff(num1=0, num2=0):
return num1-num2
output = calculate_diff()
print("Diff Between Two Number is: ", output)
In the above code, I have the calculate_diff function and that required two positional parameters num1 and num2. But I have Given Default values to both num1=0 and num2=0. So when I am using my calculate_diff function and if I am not giving value to this function then it will take this default value. and Give me output just like below.
Diff Between Two Number is: 0
And If I am passing value then it will return me a value on behalf of value. Just like below.
def calculate_diff(num1=0, num2=0):
return num1-num2
output = calculate_diff(10, 9)
print("Diff Between Two Number is: ", output)
Output Of Above code is.
Diff Between Two Number is: 1
And Now, Your error must be solved. thank You for Reading Our Article.
Conclusion
It’s all About this error. I hope We solved Your error. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you?
Also, Read
- ReferenceError: __dirname is not defined in ES module scope
- WebDriverException: unknown error: unexpected command response
- Error: Cannot find module ‘webpack/lib/rules/DescriptionDataMatcherRulePlugin’
- ImportError: cannot import name ‘docevents’ from ‘botocore.docs.bcdoc’
- Error: Cannot find module ‘dotenv’