Python - how to fix the unhashable type: 'dict' and format requires a mapping for operator tripple quotes -


how arr , myname formatted?

arr = {  'pincode':'pincode', 'username':'username', 'password':'password', 'action':'action', 'forward':'forward' } myname="myname" print """my pincode is: %(pincode)s , name is: %s !""" % {arr, myname} print """my pincode is: %(pincode)s , name is: %s !""" % (arr, myname) 

expecting output:

my pincode is: pincode , name is: myname !

getting:

typeerror: format requires mapping  typeerror: unhashable type: 'dict'  typeerror: not enough arguments format string 

this works:

arr = {  'pincode':'pincode', 'username':'username', 'password':'password', 'action':'action', 'forward':'forward' } myname="myname" print """my pincode is: %s , name is: %s !""" % (arr['pincode'], myname) 

you can use named arguments format:

print 'my pincode is: {pincode} , name is: {myname} !'.format(        myname=myname, **arr) 

note setting keyword myname value prior setting other keywords in mapping using **arr.

cheers.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -