I've come across an issue with some code I'm writing to display a list of objects that the user can select from. Each object has a given 'Size' attached to it, and each time an object is selected, that size is subtracted from the maximum allowable amount of objects the user can select.
Basically, user starts with an amount limit of 5. Each object has a size ranging from 1 to 4.
When the user first makes a selection, there are no restrictions, but after that, what the list should show is limited to objects under what amount they have left, so they shouldn't see any objects with a 'Size' of 3, if they only have an amount limit of 2 remaining.
So far, my existing code works for everything except putting those limitations in place.
set1_items = {
"Option1": {
"Common": [
{"Item Name": "A1", "Size": 3},
{"Item Name": "A2", "Size": 4}
],
"Uncommon": [
{"Item Name": "B1", "Size": 2},
{"Item Name": "B2", "Size": 1}
]
},
"Option2": {
"Common": [
{"Item Name": "C1", "Size": 3},
{"Item Name": "C2", "Size": 1}
],
"Uncommon": [
{"Item Name": "D1", "Size": 2},
{"Item Name": "D2", "Size": 4}
]
}
}
chosen_objects = []
objects_chosen = None
max_amount_limit = 5
selected_objects_size = 0
available_objects_list = []
while objects_chosen is None:
if (max_amount_limit - selected_objects_size) > 3:
available_objects_list = set1_items["Option1"]["Common"]
elif (max_amount_limit - selected_objects_size) <4:
available_objects_list = set1_items["Option1"]["Common"]?????
object_availability = available_objects_list
objects_itr = 0
for i in object_availability:
print(object_itr, end=' - ')
for key, val in i.items():
print(key, ": ", val, end='\n ')
print()
object_itr = object_itr+1
chosen_object_index = input("> ")
chosen_object_index = int(chosen_object_index)
chosen_objects.append(object_availability[chosen_object_index]["Item Name"]
selected_objects_size = selected_objects_size + object_availability[chosen_object_index]["Size"]
if max_amount_limit == selected_objects_size:
objects_chosen = "Done"
else:
continue_object_selection = input("""
You have """+str(int(max_amount_limit - selected_objects_size))+""" amounts left.
Do you want to select more objects from Set 1, or move on to Set 2?
1 - Continue with Set 1
2 - Move on to Set 2
>""")
if continue_object_selection == "1":
objects_chosen is None
elif continue_object_selection == "2":
objects_chosen - "Done"
else:
print("Not a valid choice, try again")
This is a basic, trimmed down version of my code, and I've added ????? where the code is in need of something more.
Any help would be greatly appreciated, as I haven't been able to find anything regarding appending nested dictionary items to a list based on their values.
Thank you.
Your question's example code doesn't run, but I think the only thing you're asking about is how to filter a nested dictionary.
If so, I'd use comprehensions.
Demo : https://trinket.io/python3/5c8666dcce
Edit: Examples for filtering rarity too, either dynamically or hard coded...
Or...
Demo: https://trinket.io/python3/5835bd66c1