← Back to Portfolio

Midterm Lab Task 5

Creating Class and Instantiating Objects in Python

Problem #1

In this problem, you are asked to create a User class that represents a social media user. The class should have the following attributes: first_name, last_name, followers_count, and friends_name (which should be a list). These attributes must be initialized using a constructor. You will also need to include two methods. The first method, introduce_self, should display a message saying “Hi I am {first_name} {last_name}.” The second method, view_full_profile, should print all the user’s information in this format: “Profile Name is Bradd Pitt with 10 followers. Friends are… Juan Pedro Jose.”

After creating the User class, you need to make a separate class called TestUser to test if your program works properly. This class should allow user input to be passed as parameters when creating the object. Finally, create at least two object instances to test the User class and verify that both methods function as expected.

Code


class User:
    user_count = 0

    def __init__(self, first_name, last_name, followers_count, friends_name):
        self.first_name = first_name
        self.last_name = last_name
        self.followers_count = followers_count
        self.friends_name = friends_name
        User.user_count += 1

    def introduce_self(self):
        print(f"Hi I am {self.first_name} {self.last_name}")

    def view_full_profile(self):
        print(f"Profile Name: {self.first_name} {self.last_name} with {self.followers_count} followers")
        print("Your friends are: " + ', '.join(self.friends_name))
        print()


class TestUser:
    def __init__(self):
        self.users = []

    def run(self):
        while True:
            insert = input("\nInsert a Record? [y|n]: ")
            if insert.lower() != 'y':
                break

            first_name = input("\nFirst Name: ")
            last_name = input("Last Name: ")
            followers_count = int(input("Followers: "))

            friends = []
            print("Enter friends one by one. Type 'done' when finished:")
            while True:
                friend = input()
                if friend.lower() == 'done':
                    break
                if friend.strip():
                    friends.append(friend.strip())

            user = User(first_name, last_name, followers_count, friends)
            self.users.append(user)

        print("\nHere are the Records:")
        for user in self.users:
            user.introduce_self()
            user.view_full_profile()

        print(f"There are currently {User.user_count} members in the Social Media page.")


test = TestUser()
test.run()
        

Output


Insert a Record? [y|n]: y

First Name: Justine
Last Name: Tayting
Followers: 6767
Enter friends one by one. Type 'done' when finished:
Enzo
Almea
Noel
done

Insert a Record? [y|n]: y

First Name: Enzo
Last Name: Bulanadi
Followers: 6969
Enter friends one by one. Type 'done' when finished:
Justine
Blaine
Jessie
done

Insert a Record? [y|n]: n

Here are the Records:
Hi I am Justine Tayting
Profile Name: Justine Tayting with 6767 followers
Your friends are: Enzo, Almea, Noel

Hi I am Enzo Bulanadi
Profile Name: Enzo Bulanadi with 6969 followers
Your friends are: Justine, Blaine, Jessie

There are currently 2 members in the Social Media page.
        
View PDF Here