Build an Email Simulator - Step 50

Tell us what’s happening:

Hello, I can’t figure out the main issue in my code saying Traceback (most recent call last):
File “main.py”, line 91, in
File “main.py”, line 86, in main
TypeError: User.init() takes 2 positional arguments but 3 were given. Please I need an assist to help debug the error.

Your code so far

import datetime

class Email:
    def __init__(self, sender, receiver, subject, body):
        self.sender = sender
        self.receiver = receiver
        self.subject = subject
        self.body = body
        self.timestamp = datetime.datetime.now()
        self.read = False

    def mark_as_read(self):
        self.read = True

    def display_full_email(self):
        self.mark_as_read()
        print('\n--- Email ---')
        print(f'From: {self.sender.name}')
        print(f'To: {self.receiver.name}')
        print(f'Subject: {self.subject}')
        print(f"Received: {self.timestamp.strftime('%Y-%m-%d %H:%M')}")
        print(f'Body: {self.body}')
        print('------------\n')

    def __str__(self):
        status = 'Read' if self.read else 'Unread'
        return f"[{status}] From: {self.sender.name} | Subject: {self.subject} | Time: {self.timestamp.strftime('%Y-%m-%d %H:%M')}"
class Inbox:
    def __init__(self):
        self.emails = []

    def receive_email(self, email):
        self.emails.append(email)

    def list_emails(self):
        if not self.emails:
            print('Your inbox is empty.\n')
            return
        print('\nYour Emails:')
        for i, email in enumerate(self.emails, start=1):
            print(f'{i}. {email}')


    def read_email(self, index):
        if not self.emails:
            print('Inbox is empty.\n')
            return
        actual_index = index - 1
        if actual_index < 0 or actual_index >= len(self.emails):
            print('Invalid email number.\n')
            return
        self.emails[actual_index].display_full_email()

    def delete_email(self, index):
        if not self.emails:
            print('Inbox is empty.\n')
            return
        actual_index = index - 1
        if actual_index < 0 or actual_index >= len(self.emails):
            print('Invalid email number.\n')
            return
        del self.emails[actual_index]
        print('Email deleted.\n')

class User:
    def __init__(self, name):
        self.name = name
        self.inbox = Inbox()

    def send_email(self, receiver, subject, body):
        email = Email(sender=self, receiver=receiver, subject=subject, body=bodyl)
        receiver.inbox.receive_email(email)
        print(f'Email sent from {self.name} to {receiver.name}!\n')

    def check_inbox(self):
        print(f"\n{self.name}'s Inbox:")
        self.inbox.list_emails()

    def read_email(self, index):
        self.inbox.read_email(index)

    def delete_email(self, index):
        self.inbox.delete_email(index)


# User Editable Region

def main():
    tory = User('Tory', Inbox())
    ramy = User('Ramy', Inbox())
    tory.send_email(ramy, subject='Hello', body='Hi Ramy, just saying hello!')
    ramy.send_email(tory, subject='Re: Hello', body='Hi Tory, hope you are fine.')

# User Editable Region

if __name__ == '__main__':
    main()

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36

Challenge Information:

Build an Email Simulator - Step 50

Github Link: freeCodeCamp/curriculum/challenges/english/blocks/workshop-email-simulator/68540a534c56aeabae6afd58.md at main · freeCodeCamp/freeCodeCamp · GitHub

Hi @anthonybassey :waving_hand:

The error is coming from these two lines :

You are calling the User class, defining tony and ramy as objects of the class. You should give them the arguments that User expects :

self is automatic, and you added the name, but you add a third one, which is Inbox(). The class is overwhelmed, so this causes an error.

Hope this helped you, enjoy coding :slight_smile:

Thank you @Jarvis_T
In the def init(self, name): there are two parameter which is the self and name, but the third one which is not expected is the email and that caused the File “main.py”, line 86, in main
TypeError: User.init() takes 2 positional arguments but 3 were given. I have been on this as at yesterday. I need closer insight possibly a snippet of the code to help pass the test. I tried include the third arguments which is the inbox in the parameter it didn’t work.

Actually, this 3rd argument is the one causing the error. You are told in the error :

User.init() takes 2 positional arguments

Which are self and name. But it cannot handle a third one, the Inbox().

By the way, you have here :

These mentions (subject=, body=) are useless, and they cause the tests to fail, even though the result is the same.

Please help review this, it seems to be cool somehow the error messages has gone after including the third arguments in the user parameter but still saying:

  1. You should have Tory send an email to ramy, subject Hello, and body Hi Ramy, just saying hello!
    . 2. You should have Ramy send an email to tory, subject Re: Hello, and body Hi Tory, hope you are fine.
import datetime

class Email:
    def __init__(self, sender, receiver, subject, body):
        self.sender = sender
        self.receiver = receiver
        self.subject = subject
        self.body = body
        self.timestamp = datetime.datetime.now()
        self.read = False

    def mark_as_read(self):
        self.read = True

    def display_full_email(self):
        self.mark_as_read()
        print('\n--- Email ---')
        print(f'From: {self.sender.name}')
        print(f'To: {self.receiver.name}')
        print(f'Subject: {self.subject}')
        print(f"Received: {self.timestamp.strftime('%Y-%m-%d %H:%M')}")
        print(f'Body: {self.body}')
        print('------------\n')

    def __str__(self):
        status = 'Read' if self.read else 'Unread'
        return f"[{status}] From: {self.sender.name} | Subject: {self.subject} | Time: {self.timestamp.strftime('%Y-%m-%d %H:%M')}"
class Inbox:
    def __init__(self):
        self.emails = []

    def receive_email(self, email):
        self.emails.append(email)

    def list_emails(self):
        if not self.emails:
            print('Your inbox is empty.\n')
            return
        print('\nYour Emails:')
        for i, email in enumerate(self.emails, start=1):
            print(f'{i}. {email}')


    def read_email(self, index):
        if not self.emails:
            print('Inbox is empty.\n')
            return
        actual_index = index - 1
        if actual_index < 0 or actual_index >= len(self.emails):
            print('Invalid email number.\n')
            return
        self.emails[actual_index].display_full_email()

    def delete_email(self, index):
        if not self.emails:
            print('Inbox is empty.\n')
            return
        actual_index = index - 1
        if actual_index < 0 or actual_index >= len(self.emails):
            print('Invalid email number.\n')
            return
        del self.emails[actual_index]
        print('Email deleted.\n')

class User:
    def __init__(self, name, inbox):
        self.name = name
        self.inbox = Inbox()

    def send_email(self, receiver, subject, body):
        email = Email(sender=self, receiver=receiver, subject=subject, body=body)
        receiver.inbox.receive_email(email)
        print(f'Email sent from {self.name} to {receiver.name}!\n')

    def check_inbox(self):
        print(f"\n{self.name}'s Inbox:")
        self.inbox.list_emails()

    def read_email(self, index):
        self.inbox.read_email(index)

    def delete_email(self, index):
        self.inbox.delete_email(index)

def main():
    tory = User('Tory', Inbox())
    ramy = User('Ramy', Inbox())
    tory.send_email(ramy, subject='Hello', body='Hi Ramy, just saying hello!')
    ramy.send_email(tory, subject='Re: Hello', body='Hi Tory, hope you are fine.')
if __name__ == '__main__':
    main() 

As I told earlier, the subject= and body= mentions are useless, an they cause the tests to fail. However, I’m not sure that any test will success now, since you modified the original code. Maybe you have to reset your code, so that you can take back again.

You are a soul saver, many thanks :saluting_face: