Okay, now that you've learned the basics of object-oriented programming in Python, it is time for you to create your very own classes! If you look below your Textbook class, you'll see the classes MemePage and Member. Your job is to fill them both out!
- The MemePage class represents one of the many meme pages on the internet. Your job is to fill out the following to-do list so people actually want to post memes on it, and so it has all of the properties of a proper meme page. The Member class represents a person who is a member of a MemePage, and can add and like posts on a MemePage.
To-Do
-
A constructor method for the MemePage class which does the following:
-
Sets an instance attribute members to 0, representing the number of members who are in the MemePage.
-
Sets an instance attribute topic to the input parameter of the same name.
-
Sets an instance attribute posts to an empty dictionary. The keys in this dictionary will be the titles of the posts, and the value will be the number of likes for each post.
-
A constructor method for the Member class which does the following:
-
Sets two instance attributes name and memepage to the input parameters of the same name. The memepage represents an instance of the MemePage class.
-
Sets two instance attributes activity and num_posts both to 0.
-
Increases the attribute members of the input memepage by 1, representing the addition of this member to their corresponding memepage.
-
Completion of the tag_your_friend_in_meme method so that it does the following:
-
If the input parameter friend is not an instance of the Member class, or the friend's memepage attribute is not the same as the current instance's memepage attribute, returns string: 'You cannot tag someone in a meme if they are not a member of this page.' (Hint: You can use isinstance(object, class) to check if an object is an instance of a class)
-
Otherwise it increases your activity (not your friend's) by 1 and returns the string: '@ [insert friend's name here] has been tagged!' (Hint: Look up the string format() method in Python)
-
Completion of the method post_in_page so that it does the following:
-
If the parameter of title_of_post already exists as a key inside the memepage's posts, returns the string, 'You have been banned for reposting a meme.'
-
Otherwise, it adds the title_of_post as a key inside the memepage's posts, with the value initialized to 0 (corresponding to the number of likes so far). It increases your activity and posts each by 1. It also returns the string 'Your total activity on this [insert the topic of the meme page here] page is [insert your activity value here], and your total posts to it is now [insert your posts value here].' (Hint: Look up the string format() method in Python)
-
Completion of the method like_a_post_in_page so that it does the following:
-
Increases your activity by 1
-
Increases the value of the key which corresponds to the post's title in the memepage's instance attribute posts by 1.
-
Returns the string 'Your total activity on this [insert the topic of the memepage here] page is [insert your activity value here], and the total number of likes on the post [insert title_of_post] is [insert number of likes].'
Congratulations! You have just built your very own classes!