Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error catching – incorrect number of arguments requested when calling the function

I get an interesting problem:

Let’s take such a function for example:

def SumNumbers(a,b,c,d,e,f):
  try:
    print("Marta: ",
        a,"+",b,'+',c,'+',d,'+',e,'+',f,
        '=',
        a + b + c + d + e + f)
  except TypeError:
      print("Enter the values for the 6 parameters of the program")

How we can handling this error in this case:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

SumNumbers(1,2,3)

and in this case:

SumNumbers(1,2,3,4,5,5,6,77,7,8,88,8,8,8,8)

Of course, I mean handling this bug in the function body 🙂

My attempt to intercept a TypeError is unfortunately invalid 🙁

>Solution :

Use *args:

def SumNumbers(*args):
    if len(args) != 6:
      print("Enter the values for the 6 parameters of the program")
      return
    print(f"Marta: {' + '.join(str(i) for i in args)} = {sum(args)}")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading