[Solved] Moving through Nested Dictionaries | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

4th Oct 2020, 11:57 PM
Kelly H. Milligan
Kelly H. Milligan - avatar
7 Answers
+ 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)
6th Oct 2020, 2:15 AM
‎ ‏‏‎Anonymous Guy
+ 1
where do you need help?
5th Oct 2020, 4:14 AM
‎ ‏‏‎Anonymous Guy
+ 1
Kelly H. Milligan that's great but my question still remains where do you need help? You seem to know how it works
5th Oct 2020, 4:30 PM
‎ ‏‏‎Anonymous Guy
+ 1
‎ ‏‏‎Anonymous Guy I'm having trouble understanding how the individual parts work. Like the "isinstance" part.
5th Oct 2020, 4:53 PM
Kelly H. Milligan
Kelly H. Milligan - avatar
+ 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.
6th Oct 2020, 2:18 AM
Kelly H. Milligan
Kelly H. Milligan - avatar
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
5th Oct 2020, 3:29 PM
Kelly H. Milligan
Kelly H. Milligan - avatar
0
I've added a basic version of my project into the "ladder" for reference.
5th Oct 2020, 3:39 PM
Kelly H. Milligan
Kelly H. Milligan - avatar