How to avoid "Missing 1 required positional argument" error when one of the parameters is omitted

Asked

Viewed 2,151 times

2

I have this function in Python:

def v(m1,m2): 
    print(m1) 
    print(m2)

It gives an error when I do not enter one of the parameters, for example inserted m1 = [1,2,3] and omitting m2: v([1,2,3])

Missing 1 required positional argument

How to avoid this error?

2 answers

0

Using arguments with default values may be what you want:

def f(v1=None, v2=None):
    if v1:
     print(v1)
    if v2:
     print(v2)

So if you don’t pass v2, it’s None and therefore if v2 is fake, not running the print inside.

0

Python supports named arguments i.e., Voce can define a variable name in the function arguments:

def v(arg=None, arg2=None):
    if arg:
     // corre o codigo pro primeiro argumento
    if arg2:
     // corre o codigo pro segundo argumento


v(arg2="Oi")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.