[Solved] How to call an item in dictionary from user input | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

[Solved] How to call an item in dictionary from user input

So, I converted all of my lists to dictionaries, but I still can't figure out how to get the recipe called up by a user's input. Again, please help. https://code.sololearn.com/cHt1uEEL5B80/?ref=app

3rd Oct 2020, 2:00 AM
Kelly H. Milligan
Kelly H. Milligan - avatar
16 Answers
+ 3
Its now nested so much that there's no point in just calling one thing because there's so many layers you have to get through. I would set some while loops up with try and except lines and guide the user though each layer of the dictionary and letting them select what to display on each level.
3rd Oct 2020, 5:13 AM
Slick
Slick - avatar
+ 3
Nice! And youre basically just working with strings. So you could split the result and print the list of words on a seperate line with a for loop. Keep messing with it though, thats going to be real cool, i love that game.
3rd Oct 2020, 5:13 PM
Slick
Slick - avatar
+ 3
yeah, just message me the code
3rd Oct 2020, 6:09 PM
Slick
Slick - avatar
+ 2
Kelly H. Milligan I'm sorry but I'm afraid I can't help much without having firstly understood what your code was supposed to do. I was about to suggest you to use nested dictionary (dictionary of dictionaries) just so that we can iterate the container dictionary, searching for a given input in each of the sub-dictionaries. But then again, I'm not sure whether if it was the right approach. I'm sticking around here, so if you got some more to elaborate, just reply or ping me.
3rd Oct 2020, 3:16 AM
Ipang
+ 1
What input should be provided? And what output is expected given that input?
3rd Oct 2020, 2:46 AM
Ipang
+ 1
Kelly H. Milligan I see titanium in basic_materials, so when input is "titanium" then output should be "basic_materials" or ("1 metal salvage")? You have many dictionaries there, are you wanting to search through all the dictionaries for the given input? I still am not clearly understanding ...
3rd Oct 2020, 2:54 AM
Ipang
+ 1
Ipang I really am not sure what I'm doing. I'm doing the best I can with what I think I've learned. It's very messy, I know. If you input 'titanium', the resulting output would be 'titanium: 1 metal salvage' I have been tinkering with it, it has a few less dictionaries and a few more lists. I can't decide which to use. 😤🤷
3rd Oct 2020, 3:00 AM
Kelly H. Milligan
Kelly H. Milligan - avatar
+ 1
@Ipang It is a collection of recipes from the game Subnautica. Each recipe is created from a station (fabricator, etc.) and has set required materials (1 metal salvage for titanium), which are already contained in dictionaries. Some stations have multiple categories with themed recipes (water, electronics, etc.) and are categorized accordingly. What I want is to input 'titanium' and have it return something like '1 metal salvage' or 'titanium: 1 metal salvage' or 'distilled water' >>>'1 bladderfish' or 'filtered water = 1 bladderfish'. Does that help?
3rd Oct 2020, 3:26 AM
Kelly H. Milligan
Kelly H. Milligan - avatar
+ 1
Kelly H. Milligan I don't know that game, but never mind. I'll just try to work on that code hopefully to achieve the goal. I see you changed the code ... 👍
3rd Oct 2020, 3:39 AM
Ipang
+ 1
@Ipang I tried to clean it up to make it less confusing for myself. =)
3rd Oct 2020, 3:40 AM
Kelly H. Milligan
Kelly H. Milligan - avatar
+ 1
Yes I can see that ... Anyways, I gotta go for now, got a thing to do. Be back later ...
3rd Oct 2020, 3:43 AM
Ipang
+ 1
Kelly H. Milligan I agree with Slick here, the grouping approach added complexity in the code. I tried to edit the code in attempt to understand how the dictionaries are grouped (categorised), but I was distracted due to indentation and size of code. Got something in mind to simplify this? let's discuss ...
3rd Oct 2020, 6:10 AM
Ipang
+ 1
Not really simplify, but handle it yeah. At the bottom of your updated code is a layered while loop to display and go into each layer of the dictionary. To exit, would just be the opposite process of setting a special phrase to be the "back" phrase and when used, sets the current levels l"x" attribute to False, breaking out of that point of the loop, going to the outside one. Play around with it in pydroid https://code.sololearn.com/cz7gGvOLMFO1/?ref=app
3rd Oct 2020, 6:48 AM
Slick
Slick - avatar
+ 1
Just downloaded pydroid, got what you meant. I like the way it works for the most part, although the 'fabricator' starts breaking once you get to the third sub-category. I think I can fix that, though. Just need to add another sub-category. I just wish I knew how to get it to print the end results horizontally. I'll have to look into that. https://code.sololearn.com/cHt1uEEL5B80/?ref=app
3rd Oct 2020, 5:10 PM
Kelly H. Milligan
Kelly H. Milligan - avatar
0
Ipang, if a user wants to find the recipe for 'titanium', they input the word 'titanium' and said recipe is outputted
3rd Oct 2020, 2:47 AM
Kelly H. Milligan
Kelly H. Milligan - avatar
0
So, once I get to 'basic materials', it starts spamming (horizontally, yay, accident) the results. Help?
3rd Oct 2020, 6:03 PM
Kelly H. Milligan
Kelly H. Milligan - avatar