Python Forum
TypeError: Missing required positional arguments
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: Missing required positional arguments
#1
I have to complete this task for my class and Im stuck on this exercise because it always gives me this same error:
TypeError: calculate() missing 4 required positional arguments: 'a', 'b', 'c', and 'd'

The exercise is to create a function so that a - (b**2 / (c - d * (a + b))) creates a result. The exercise also states that I must use the "variables above" so they are already defined in the exercise by a=1 b=2 c=3 d=4 (so the way I understood it these would already be set as global variables)

So what I did looks something like this (apart from variables which are already put into):
a=1
b=2
c=3
d=4

def calculate(a,b,c,d):
    res = a - (b**2 / (c - d * (a + b)))
    return res
Unfortunately I always get the error mentioned above, apart from when I define the function as "def calculate(a=1,b=2,c=3,d=4) which I dont think is the right solution.
Can anybody explain what Im doing wrong, Im very new to Python...
Thank you Smile
Reply
#2
When you call the function you must pass the fourth variables as arguments
a=1
b=2
c=3
d=4
 
def calculate(a,b,c,d):
    res = a - (b**2 / (c - d * (a + b)))
    return res
print(calculate(a, b, c, d))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
If you do this:
def calculate(a=1, b=2, c=3, d=4):
    value = a-(b**2/(c-d*(a+b)))
    print(f'calculate({a}, {b}, {c}, {d}) = {value}')
    return value

calculate()
Output:
calculate(1, 2, 3, 4) = 1.4444444444444444
It calls the function calculate with a, b, c, d having the values 1, 2, 3, 4. The definition of the function specifies default values for each of the arguments and defines them as named arguments or kvargs. Since there are default values specified for all the arguments I can call the function without any arguments.

I can also call the function and specify all the arguments like this:
calculate(4, 3, 2, 1)
Output:
calculate(4, 3, 2, 1) = 5.8
And I can specify some of the arguments by using their name:
calculate(b=12)
Output:
calculate(1, 12, 3, 4) = 3.938775510204082
You do not have to use default values and named arguments. You can write the same function like this:
def calculate(a, b, c, d):
    value = a-(b**2/(c-d*(a+b)))
    print(f'calculate({a}, {b}, {c}, {d}) = {value}')
    return value

calculate()
Error:
Traceback (most recent call last): File "C:\Users\hystadd\Documents\python\sandbox\junk.py", line 6, in <module> calculate() TypeError: calculate() missing 4 required positional arguments: 'a', 'b', 'c', and 'd'
There are no default values, so I have to provide all the arguments
calculate(1, 2, 3, 4)
Output:
calculate(1, 2, 3, 4) = 1.4444444444444444
I can still use names, but I have to provide 4 argument values.
calculate(1, 2, 3, d=7)
Output:
calculate(1, 2, 3, 7) = 1.2222222222222223
Python requires that all position arguments (value only, not name=value) must appear in the argument list first, followed by the named arguments.
calculate(1, b = 7, 3, 4)
Error:
SyntaxError: positional argument follows keyword argument
Some additional comments:
I put a print in my function to show how the values in the function call get mapped to the arguments inside the function. It is generally not a good idea to print from inside a function unless the purpose of the function is printing.

Sometimes it does not make any sense to use named arguments. Do any of the arguments in this function have commonly used values? For a this function it may make sense to only use position arguments, thus forcing the caller to supply values for all the arguments.

If you use named arguments, you should supply useful default values. 1, 2, 3, 4 are probably not common values for this function.

calculate is a bad function name. What is the calculated value? How would I use it? Name the function after what it does or what it is used for. A void using generic names like "function" or "calculate".
Reply
#4
(Sep-25-2020, 05:41 PM)buran Wrote: When you call the function you must pass the fourth variables as arguments
a=1
b=2
c=3
d=4
 
def calculate(a,b,c,d):
    res = a - (b**2 / (c - d * (a + b)))
    return res
print(calculate(a, b, c, d))

Thank you for your reply and so sorry for my mistake, will do it right next time!

(Sep-25-2020, 05:56 PM)deanhystad Wrote: If you do this:
def calculate(a=1, b=2, c=3, d=4):
    value = a-(b**2/(c-d*(a+b)))
    print(f'calculate({a}, {b}, {c}, {d}) = {value}')
    return value

calculate()
Output:
calculate(1, 2, 3, 4) = 1.4444444444444444
It calls the function calculate with a, b, c, d having the values 1, 2, 3, 4. The definition of the function specifies default values for each of the arguments and defines them as named arguments or kvargs. Since there are default values specified for all the arguments I can call the function without any arguments.

I can also call the function and specify all the arguments like this:
calculate(4, 3, 2, 1)
Output:
calculate(4, 3, 2, 1) = 5.8
And I can specify some of the arguments by using their name:
calculate(b=12)
Output:
calculate(1, 12, 3, 4) = 3.938775510204082
You do not have to use default values and named arguments. You can write the same function like this:
def calculate(a, b, c, d):
    value = a-(b**2/(c-d*(a+b)))
    print(f'calculate({a}, {b}, {c}, {d}) = {value}')
    return value

calculate()
Error:
Traceback (most recent call last): File "C:\Users\hystadd\Documents\python\sandbox\junk.py", line 6, in <module> calculate() TypeError: calculate() missing 4 required positional arguments: 'a', 'b', 'c', and 'd'
There are no default values, so I have to provide all the arguments
calculate(1, 2, 3, 4)
Output:
calculate(1, 2, 3, 4) = 1.4444444444444444
I can still use names, but I have to provide 4 argument values.
calculate(1, 2, 3, d=7)
Output:
calculate(1, 2, 3, 7) = 1.2222222222222223
Python requires that all position arguments (value only, not name=value) must appear in the argument list first, followed by the named arguments.
calculate(1, b = 7, 3, 4)
Error:
SyntaxError: positional argument follows keyword argument
Some additional comments:
I put a print in my function to show how the values in the function call get mapped to the arguments inside the function. It is generally not a good idea to print from inside a function unless the purpose of the function is printing.

Sometimes it does not make any sense to use named arguments. Do any of the arguments in this function have commonly used values? For a this function it may make sense to only use position arguments, thus forcing the caller to supply values for all the arguments.

If you use named arguments, you should supply useful default values. 1, 2, 3, 4 are probably not common values for this function.

calculate is a bad function name. What is the calculated value? How would I use it? Name the function after what it does or what it is used for. A void using generic names like "function" or "calculate".

Thank you for this elaborate reply!! Unfortunately the value for a,b,c and d (1,2,3,4) are already given, aswell as the function name calculate so I cannot change that...
Anyway I tried using your tips and even though the console gives me an output, the exact same type error remains (I attached pictures to give an exact view of this)
/Users/Lia/Desktop/Screenshot 2020-09-25 at 20.17.31.png
Reply
#5
We cannot see your screenshot. Please post your error messages as text instead as a screenshot.

You also need to show us how you are calling your function. If your function does not provide default values for the arguments (your example is not doing this), then you have to provide argument values in the function call. This is done by placing the values in the parenthesis.
calculate(1, 2, 3, 4) is how you call function "calculate" with arguments 1, 2, 3, 4. In your first post you define some variables; a=1, b=2, c=3, d=4, but this does nothing to set the variables for the function call. Those variables are never used,
Reply
#6
Aaaand again sorry for the screenshot text, my mistake:
Here is my code, which still gives me the exact same error
a = 1
b = 2
c = 3
d = 4

def calculate(a,b,c,d):
    res = a - (b**2 / (c - d * (a + b)))
    return res

print(calculate(a, b, c, d))
Error:
Traceback (most recent call last): File "/usr/src/public/tests.py", line 8, in test_output_is_number output = script.calculate() TypeError: calculate() missing 4 required positional arguments: 'a', 'b', 'c', and 'd'

(Sep-25-2020, 06:40 PM)deanhystad Wrote: We cannot see your screenshot. Please post your error messages as text instead as a screenshot.

You also need to show us how you are calling your function. If your function does not provide default values for the arguments (your example is not doing this), then you have to provide argument values in the function call. This is done by placing the values in the parenthesis.
calculate(1, 2, 3, 4) is how you call function "calculate" with arguments 1, 2, 3, 4. In your first post you define some variables; a=1, b=2, c=3, d=4, but this does nothing to set the variables for the function call. Those variables are never used,

a = 1
b = 2
c = 3
d = 4


def calculate(a,b,c,d):
    res = a - (b**2 / (c - d * (a + b)))
    return res

calculate(1,2,3,4)
well even if I call the functions with the variables I still get that error:
Error:
Traceback (most recent call last): File "/usr/src/public/tests.py", line 8, in test_output_is_number output = script.calculate() TypeError: calculate() missing 4 required positional arguments: 'a', 'b', 'c', and 'd'
Reply
#7
It's the call to calculate in that tests.py module that's not passing the right number of arguments. Are you supposed to modify that to pass the arguments, or ...?
Reply
#8
The problem is happening in tests.py. I'm guessing that your assignment is to provide default values for the calculate function so the test can call your function without any arguments. Instead of variables, the 1, 2, 3, 4 are the default values for a, b, c and d.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: __init__() missing 1 required positional argument: 'successor siki 1 4,302 Mar-08-2021, 02:05 PM
Last Post: Larz60+
  Missing 1 required positional argument in python code edwinostby 7 9,933 Jan-19-2021, 12:52 PM
Last Post: Serafim
  Missing positional arguments error?? hhydration 2 2,148 Oct-01-2020, 05:33 AM
Last Post: buran
  missing positional argument error programmert 1 2,821 Oct-18-2019, 11:05 AM
Last Post: Larz60+
  missing 1 required positional argument jedmond2 4 6,691 Sep-19-2019, 12:00 PM
Last Post: jefsummers
  missing 1 required positional argument mcgrim 10 19,756 May-07-2019, 09:02 PM
Last Post: Yoriz
  TypeError: __init__() missing 3 required positional arguments Pythonhelp82 6 23,128 Jan-24-2019, 04:25 AM
Last Post: Pythonhelp82
  TypeError: method missing 1 positional argument koolinka 4 5,028 Nov-18-2018, 04:53 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020