When I try to make a local variable to pull my mission data using the list index, it fails to find it. I can't figure out why. Can anyone help me figure out what I'm doing wrong? I also provided the result of the "call_log" function at the end of the block.
Probably I should let others with proper knowledge answer and I am not sure if this is helpful or not (and I do not quite understand what I am writing), but I also had similar problem. I do not quite remember what really happened, but it had something to do with how data is stored in dictionaries. I had to introduce some json decoding when I was reading back a list from a dictionary. I attach a screenshot of part of my code.
How do I check to see if I have a dictionary or json string? I created a dictionary with keys (what are being called to the text boxes and labels) and then passed the house to this page as the start value text. The calllog function is showing you the value of the selection index, and the value of thelist item holding the dictionary that I am trying to get keys out of on the page.
The first key in the dictionary is 1. The selection index is 1. But it can't find it.
Would it be better to rewrite the function that imports the house data from the CSV file (which the mission list gets added to later) as solely using dictionaries instead of a dictionary of lists where the 4th item in the list has another dictionary? Would that make the data easier for the mit app inventor to parse correctly when it reads or stores it?
Right now, my current data is created on the previous screen and stored in a tinydb like this:
TinyDB key: Houses
value: Dictionary {
Key: House
Value: List [
1: Integer
2: List [2-4 integers]
3: String
4: Dictionary {
Key: 1 Value: Dictionary {
Key: difficulty Value: Integer
Key: mission type Value: Integer
Key: do for Value: string
Key: do to Value: string
}
}
]
}
Thank you for this insight! If you look at the call log block, you'll see that it mirrors current_mission_data all the way up to the final get value for key block. I was attempting to illustrate that I had been able to successfully follow it all the way to there, but I can't seem to find the dictionary at that value (that is shown to exist in the log) to pull data from it.
This does not answer your original question, but I took this as a learning opportunity to create a TinyDB database (with key "Houses" and a list of length 1 of a "House" dictionary, according to your description).
I could recall (by pressing Button 2) the "difficulty value" entry from the innermost dictionary when I used Json conversion when reading the TinyDB data, but I got an error message when I tried to access this data without Json conversion using Button 1.
Unless you have saved this to tinydb directly as a dictionary, this will be treated as a json string when you call it back. With your data in this format, you should be able to traverse the dictionary at any level and return the data you require.
To be honest, I'm still not sure what was wrong. Do it (I didn't know what this was and had to look it up!) was definitely a good plan, and I ended up reworking my code so that I only had lists stored inside of lists. My code now works! I'm assuming there's a weird bug or something in how the data is parsed that is causing it to lose track of what it's trying to read when you nest dictionaries in lists in dictionaries etc.
Probably a best practice to avoid doing this whenever possible. Keep to storing dictionaries in dictionaries, and storing lists in dictionaries whenever possible.
You can have a dictionary inside a list, and you can have a list inside a dictionary. You just have to get the syntax correct, and know when to use a list block and when to use a dictionary block when traversing the "json"
Yeah, I think this is what I posted above with my data structure? Unfortunately, this entire structure is created (then added to) as a dictionary in a tinydb directly. Here is the block where I create the initial database!
Also, I ended up rewriting how I store the final dictionaries as lists, and the code works now, so at this point everything we talk about is theoretical. I did want to take a minute to reply to you
Also I want to recognize that you've responded directly to both issues I've had to ask for help about, and I really appreciate you and @ABG for that! I am incredibly grateful.
Right, and in the original block that I shared, you can see that I was able to successfully navigate to the dictionary in question. However, when I attempted to pull a key value from that dictionary that matched, it returned nothing. If I attempted to pull it as a list, it said that I had a poorly formed list of pairs (I think?). When I attempted to decode it as json, I also received an error message.
Here is the code I use to generate missions. As you can see, it now creates the missions as a list of lists instead of a dictionary of dictionaires, and the information inside that list is now also a list instead of a dictionary.
I can now successfully pull the data for each mission, although I had to do some fancy footwork to translate the selection index to always match the mission index inside each house.
Like I said, I don't know what was going wrong. The data was being successfully created and stored inside the list as a dictionary of dictionaries of missions with keys equal to each value that I wanted to generate for that mission type. However, I couldn't retrieve it. As far as I can tell, I was doing everything correctly at the time. Once I shifted to creating and storing the information in lists, it works fine.