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'