Putting it together
- Python course
- Intermediate
- Lesson 63/116
- 6 min
Combine the Blog class with a menu loop.
Put all the pieces together
In the final lesson you combine all pieces into a working command-line flow. You define a show_menu function; inside it you create a blog, add three posts and call the search. You print the post count and the search result.
In a real app this flow would branch into menu choices; here we use fixed data and verify the output. The point is to see how the classes and methods work together.
Practice Write the show_menu function
The classes above are ready. Now write def show_menu():. Inside it create blog = Blog(), add three posts (two by "Ayşe", the third by "Ali"). Then add print("Post count:", len(blog.posts)) and print("Ayşe's posts:", len(blog.search_by_author("Ayşe"))). Finally call show_menu().
Starter code
from dataclasses import dataclass, field
@dataclass
class Post:
title: str = ""
content: str = ""
author: str = ""
comments: list = field(default_factory=list)
def add_comment(self, comment):
self.comments.append(comment)
@dataclass
class Comment:
author: str = ""
text: str = ""
@dataclass
class Blog:
posts: list = field(default_factory=list)
def add_post(self, post):
self.posts.append(post)
def search_by_author(self, author):
return [p for p in self.posts if p.author == author]
# Define the show_menu function
# Call the functionSample solution
from dataclasses import dataclass, field
@dataclass
class Post:
title: str = ""
content: str = ""
author: str = ""
comments: list = field(default_factory=list)
def add_comment(self, comment):
self.comments.append(comment)
@dataclass
class Comment:
author: str = ""
text: str = ""
@dataclass
class Blog:
posts: list = field(default_factory=list)
def add_post(self, post):
self.posts.append(post)
def search_by_author(self, author):
return [p for p in self.posts if p.author == author]
def show_menu():
blog = Blog()
blog.add_post(Post(title="First", author="Ayşe"))
blog.add_post(Post(title="Second", author="Ali"))
blog.add_post(Post(title="Third", author="Ayşe"))
print("Post count:", len(blog.posts))
print("Ayşe's posts:", len(blog.search_by_author("Ayşe")))
show_menu()Expected output
Post count: 3 Ayşe's posts: 2
Review of the project
In this project you defined three classes: Post, Comment and Blog. Each does one thing well: Blog manages the collection, Post holds comments, and Comment only carries data.
You used a list comprehension in the search method — a pattern shorter than a loop with a condition. Thanks to dataclasses you declared many fields without writing a constructor by hand. This structure scales to larger apps.
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does integrating the project achieve?
- All pieces work together; a complete app emerges Correct answer
- Less code is written; shortcuts activate
- Classes become redundant and can be deleted
- The program automatically speeds up
Explanation: Integration builds a complete flow where every piece works together.
2 Complete the menu call
____()
show_menu()
Explanation: show_menu() runs the function.
3 Match the project classes
| Term | Meaning |
|---|---|
Blog |
Manages the post collection |
Post |
Holds one post and its comments |
Comment |
Carries comment data |
search_by_author |
Searches by author |