0
[Solved] Moving through Nested Dictionaries
So, I have a custom function ("dive" was made by Slick, "ascend" by me) that uses input to go down into a dictionary from a dictionary. My problem is that I'm having trouble understanding how the individual parts work. I want to have a second function that effectively reverses it, though I can't figure it out. https://code.sololearn.com/ct9KUkpvFn0v/?ref=app
7 ответов
+ 1
Kelly H. Milligan
In dive function, first of all, program iterates through dictionary and prints all the available keys.
User enters one of the option from the printed list
Program checks if the option entered by user is a key in dictionary and if that key itself is a dictionary (using isinstance() function)
If yes, program calls the dive function with key as argument else program prints value of the key provided by user.
Now, here's a problem, if the key given by user is not present in dict, then you get a key not found error.
What you could do is add an elif statement which prints some error message and calls dive with same dict as argument, if key is not present in dictionary
(isinstance() checks if type of first argument is same as second argument. Second argument could be an object or a data type)
+ 1
where do you need help?
+ 1
Kelly H. Milligan that's great but my question still remains where do you need help?
You seem to know how it works
+ 1
Anonymous Guy
I'm having trouble understanding how the individual parts work. Like the "isinstance" part.
+ 1
Oooh, okay, thanks for the explanation. I understood it, must mean I learned something since I asked the question.
I went to stack overflow and someone on there provided me with a working function.
0
Anonymous Guy
This "ladder" is a small section of a rather complicated project in working on (included below).
It displays the main categories, then, upon user input, displays the next set, and the next, until it displays the values. You input the name of the category, then sub-category, then the key of the value you want.
https://code.sololearn.com/cHt1uEEL5B80/?ref=app
0
I've added a basic version of my project into the "ladder" for reference.