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
Post a Comment