0

I'm new in Python. I try to implement a function that could pass args and use %s to print the keys and the values in dict args.But it tells me is invalid to use %. That really confused me. Thank you for telling me why.

>>> def person(name, age, **args):
print('name:', name, 'age:', age)
for i in args.keys():
    print('%s:%s', % (i, args[i]))

SyntaxError: invalid syntax

The following code is I try to use format instead of %s to print the keys and values of args. But it keeps telling me args is a NoneType. Then I print args in normal way("args:", args), it work.I'd like to know why it keeps saying that.THANK YOU!

>>> def person(name, age, **args):
print('name:', name, 'age:', age)
for i in args.keys():
    print('{}:{}').format(i, args[i])

>>> person('Joey', 20, weight=60, length=172)
name: Joey age: 20
{}:{}
Traceback (most recent call last):
File "<pyshell#79>", line 1, in <module>
   person('Joey', 20, weight=60, length=172)
File "<pyshell#78>", line 4, in person
   print('{}:{}').format(i, args[i])
AttributeError: 'NoneType' object has no attribute 'format'
T Joey
  • 19
  • 1
  • 1
    In the second case your format should be inside your call to `print` - e.g. `print('{}:{}'.format(i, args[i]))` – asongtoruin May 01 '17 at 10:53

4 Answers4

1

You have additional comma, here you go:

def person(name, age, **args):
    print('name:', name, 'age:', age)

    for i in args.keys():
        print('%s:%s' % (i, args[i]))

person('Joey', 20, weight=60, length=172)
zipa
  • 27,316
  • 6
  • 40
  • 58
1

your syntax is wrong, you need to format the string inside the print:

print('{}:{}'.format(i, args[i]))
Netwave
  • 40,134
  • 6
  • 50
  • 93
1

You are having an additional comma , after your string. Remove that and your code will work fine. Also, it is better to use dict.item(...) instead of dict.keys(..) for your case. Below is the sample code:

>>> kwargs = {'name': 'My name', 'age': 'My Age', 'Address': 'My Address'}

#                   v `dict.items()` to get tuple of 
#                   v (`key`, `value`) pair
>>> for i in kwargs.items():
...     print('%s:%s' % (i))
...     #            ^ No comma          
Address:My Address
name:My name
age:My Age

Also, as per the general convention your variable name should be kwargs instead of args. Please read: *args and **kwargs?

Community
  • 1
  • 1
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
0

The way you define your function:

def person(name, age, **args):

means that you want 2 mandatory arguments + keyword arguments.

So, you need to do something like:

person(argument_one, argument_two, keyword_argument_one=keyword_argument_one_value, keyword_argument_two=keyword_argument_two_value)

whereas you are doing print('name:', name, 'age:', age)

which are 4 arguments: 'name:', name, 'age:', age.

It's not actually connected to the % or .format function. This is Python basic syntax.

Then, you have another issue:

print('{}:{}').format(i, args[i])

You are calling the .format method on the print(...) output, which is None. Therefore, you see the error "NoneType has no .format method".

So, simply do:

print('{}:{}'.format(i, args[i]))

to fix it.

Markon
  • 4,480
  • 1
  • 27
  • 39