I have a nested dictionary containing both the title and the number of pages of a book. I want to write a function that gets as first argument the dictionary to search in and as second one a page number (as string). The function will then search through the dictionary and print all the titles of book which have the same number of pages as in the second argument.
Here's an example of my dictionary:
text = {
1: {
1: {"ch.name": "The Boy Who Lived", "pages": "146"},
2: {"ch.name": "The Vanishing Glass", "pages": "126"},
},
2: {
1: {"ch.name": "The Worst Birthday", "pages": "129"},
2: {"ch.name": "Dobby's Warning", "pages": "125"},
},
}
I have tried the following:
def Name(text,pages):
pages=pages
for key1, value1 in text.items():
for key2, value2 in value1.items():
output = key1,key2,value2['ch.name'],value2['pages']
output
if pages is pages:
print(f"{value2['ch.name']}")
Name(text, '125')
The result is:
The Boy Who Lived
The Vanishing Glass
The Worst Birthday
Dobby's Warning
However, the result had to the following, because it is the only book with exactly 125 pages:
Dobby's Warning