0

I need to define a function that may or may not take one argument of the 3 arguments defined in a function. However, I get an error message as invalid syntax.

Now, if I make my third argument as variable [value3], I get an error message as 'float' object is not iterable.

Also, I have realized that when all the arguments are passed, it creates a tuple, which is unfavorable.

Could someone help me solve the problem?

def createValues(value1, *value2, value3):
    value = dict()
    value["VALUE1"] = value1
    value["VALUE2"] = value2
    value["VALUE3"] = value3

    print (value["VALUE1"],value["VALUE1"],value["VALUE1"])


createValues(2000,21000001,1)
createValues(2000,,1)
1
  • Can you please make examples how your function is supposed to behave when called with 0, 1, 2, 3 arguments? Commented May 23, 2017 at 9:00

3 Answers 3

3

What you want is default arguments. This allows your function to be called by passing only a part of its parameters, and defaults the other to a pre-defined value.

For instance:

def createValues(value1=12, value2=27, value3=42):
    value = dict()
    value["VALUE1"] = value1
    value["VALUE2"] = value2
    value["VALUE3"] = value3

    print (value["VALUE1"],value["VALUE1"],value["VALUE1"])

will allow you to call your function by either of the following ways:

>>> createValues(1, 2, 3)
1, 2, 3
>>> createValues()
12, 27, 42
>>> createValues(value2=1)
12, 1, 42
>>> createValues(0, 5)
0, 5, 42

Since you seem confused with the * unary operator, I suggest that you read a bit about arguments unpacking (for example, check this post).

Besides, using two commas as in createValues(2000,,1) is in no way a valid syntax in Python.

Sign up to request clarification or add additional context in comments.

Comments

0

From my beginners point of view, I see two ways :

Using lists

The arg of createValues become a list so you have as many values as you want.

def createValues(values):
    value = dict()
    #Inserting
    for val in iter(values):
        index = str(values.index(val))
        value["VALUE"+index] = val

    #Printing
    for (key, value) in value.iteritems(): 
        print ("Key : " + key + " | Value : " + value)


createValues([2000,21000001,1])
createValues([2000,1])

Using default value

3 args for your specific case

def createValues(value1, value3, value2 = None):
    value = dict()
    value["VALUE1"] = value1
    if value2 is not None:
        value["VALUE2"] = value2
    value["VALUE3"] = value3

    print (value["VALUE1"],value["VALUE1"],value["VALUE1"])


createValues(2000,21000001,1)
createValues(value1 = 2000, value3 = 1)

Comments

0

You cannot call a function like this:

createValues(2000,,1)

You can however check if the argument is None and work with that.

def createValues(value1, value2, value3):
    value = dict()
    value["VALUE1"] = value1 or 0
    value["VALUE2"] = value2 or 0
    value["VALUE3"] = value3 or 0

    print (value["VALUE1"],value["VALUE1"],value["VALUE1"])

Now, you can use the function like this:

createValues(2000, None, 1)

I have used the default value 0 for any missing argument.

Also, the arguments are not being converted into a tuple. You are packing the arguments into a tuple when printing. Try this instead (note the lack of parenthesis):

print value["VALUE1"], value["VALUE1"], value["VALUE1"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.