[dgplug-users] Need Help in understanding the Error Message
Chaitanya Anil Kukde
chaitanya.kukde at gmail.com
Tue Feb 28 01:33:43 PST 2017
From what I understand, the function total is defined using positional
parameters, while 'name="Adi"' refers to usage of keyword arguments
which is (as I understand) not allowed and this is the cause of the
error. Relevant material which might help in understanding the
underlying mechanisms , .
The following modification in the code worked for me -
def total(*args, **kwargs):
print("%s has total money of Rs %d/- " %(kwargs['name'],
print("%s's piggy bank has no money" %kwargs['name'])
>>> total(*(3000, 10, 45), name="John Doe")
John Doe has total money of Rs 3055/-
Let me know your thoughts. Interesting question btw :)
On 02/27/2017 03:07 PM, Pabitra Pati wrote:
> Hi All,
> I want to understand the error message I am getting.
> Below is my code piece :-
> def total(name, *args):
> if args:
> print("%s has total money of Rs %d/- " %(name, sum(args)))
> print("%s's piggy bank has no money" %name)
> I can call this method passing the extra arguments inside *().
> I know the correct way of passing the arguments. But, I am passing value
> for 'name' in form of param=value, *intentionally*, so that it throws me
> error. However, I am unable to understand the below error message :-
> >>> total(name="Adi", *(1, 2, 10) )
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: total() got multiple values for keyword argument 'name'
> How Python is evaluating the above call, that it's getting multiple values
> for the parameter 'name'?
> Any help is appreciated.
> Users mailing list
> Users at lists.dgplug.org
More information about the Users