forked from JoinCODED/TASK-Python-Dictionary_Lists
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
74 lines (53 loc) · 1.84 KB
/
functions.py
File metadata and controls
74 lines (53 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from books import books
#print(books[0])
# number_of_authors(book)
# recieves a book dictionary
# returns the number of authors that the book has
def number_of_authors(book):
return len(book['authors'])
# print(number_of_authors(books[4]))
# get_book_by_id(book_id, books)
# # receives a book id
# # recieves a list of book dictionaries
# # returns the book dictionary with the same id as the book_id provided
def get_book_by_id(book_id, books):
for book in books:
if book_id == book['id']:
return book
# print(get_book_by_id(38, books))
# add_summary_to_book(summary, book)
# receives a book dictionary
# recieves a summary string
# adds the summary to the book dictionary
# return the book dictionary
def add_summary_to_book(summary, book):
book['Summary'] = summary
return book
# print(add_summary_to_book("this is a good book about", books[0]))
# CHALLENGE 1
# get_book_property(property, book)
# receives a book dictionary
# recieves a property (string)
# return the book property
def get_book_property(property, book):
return book [property]
print(get_book_property("color", books[0]))
print(get_book_property("title", books[1]))
# CHALLENGE 2
# calculate_available_books(books)
# receives a list of books
# return a new list of unavailable books
def calculate_not_available_books(books):
return [book for book in books if not book["available"]]
# print(calculate_not_available_books(books))
# CHALLENGE 3
# get_book_by_author_name(author_name, books)
# receives a author name (string)
# recieves a list of book dictionaries
# returns the book dictionary that contains an author with the author name provided
def get_book_by_author_name(author_name, books):
for book in books:
if author_name == book['authors']:
return book
#not working :(
print(get_book_by_author_name("Neil Gaiman", books))