I have two nested dictionaries inside the DEFAULTS dictionary: one is called PASSWRD and it has pairs depicting specific messages and another called COMMANDS, listing the commands I wish to use in my program, and a brief description of some key-pairs. One of those key-pairs is "Password", to which it is assigned a number.
Now, I am trying to go through a small loop that matches the "Password" key-pair value to one on PASSWRD.
When I try:
for command, stuff in DEFAULTS["COMMANDS"].items():
print(f"\nCommand: {command}")
print(f'{stuff["Definition"]}')
it lists all the commands and Definitions nicely. Problem start when I add the following line:
print(f'{stuff["Password"]}')
it delivers the following error message: KeyError: 'Password'
Any ideas why this error is produced?
The final idea would be to produce something like this:
print(DEFAULTS["PASSWRD"][DEFAULTS["COMMANDS"][{stuff}]["Password"]])
which does not work. However,
print(DEFAULTS["PASSWRD"][DEFAULTS["COMMANDS"]["ZV"]["Password"]])
works nicely
You can find the MWE below.
DEFAULTS = {
"PASSWRD" : {
0 : "None",
1: "Requires standard password",
2: "Requires factory password",
},
"COMMANDS" : {
"ZS" : {
"Type" : "SETUP",
"Max Parameters Required" : 1,
"Parameters" : "[,n]",
"Definition" : "Set/Get Seeder delay",
"Password": 0
},
"ZV" : {
"Type" : "SETUP",
"Max Parameters Required" : 1,
"Parameters" : "[,n]",
"Definition" : "Set/Get Variable Sync delay",
"Password": 0
},
}
}
for command, stuff in DEFAULTS["COMMANDS"].items():
print(f"\nCommand: {command}")
print(f'{stuff["Definition"]}')
# print(f'{stuff["Password"]}')
print(DEFAULTS["PASSWRD"][DEFAULTS["COMMANDS"]["QD"]["Password"]])
CodePudding user response:
stuff
is the current dictionary in the COMMANDS
iteration, it's not a key of anything. So use stuff["Password"]
to get the Password from that dictionary.
for command, stuff in DEFAULTS["COMMANDS"].items():
print(f"\nCommand: {command}")
print(f'{stuff["Definition"]}')
print(DEFAULTS["PASSWRD"][stuff["Password"]])