Build an Email Simulator - Step 45

Tell us what’s happening:

Can you help me identify where the error in my code lies? I keep getting an error message after running it, which reads: “You should add a print statement in the send_email method that confirms the email was sent.”

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 User:
    def __init__(self, name):
        self.name = name
        self.inbox = Inbox()


# User Editable Region

    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 [{eamil.sender.name}] to [{email.receiver.name}]!\n")


# User Editable Region


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')

Your browser information:

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

Challenge Information:

Build an Email Simulator - Step 45

Welcome to the forum @weiw_juan

You do not need to use square brackets in your f-string.

You have a typo, however check how you access sender and receiver names.

Happy coding

I’m having the same issue, same error message:

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=body)
        receiver.inbox.receive_email(email)
        print(f'Email sent from {self.name} to {email.receiver.name}!\n')

```

I also tried email.sender.name instead of self.name. Using Firefox 147.0.2 (aarch64) (“Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:147.0) Gecko/20100101 Firefox/147.0”) if that’s relevant.

please @a.bee.in.c create your own topic

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Help button located on the challenge.

The Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

Copying the relevant line from the next exercise worked:

    print(f'Email sent from {self.name} to {receiver.name}!\\n')

Even though the other variations I tried give the same result.

print(f'Email sent from {self.name} to {receiver.name}!\n')
print(f"Email sent from {email.sender.name} to {email.receiver.name}!\n")
print(f'Email sent from {self.name} to {email.receiver.name}!\n')

All output the same thing:

Email sent from b to c!

Is it a bug that it’s not counting the other variations as correct? Or if not, why not?

send_email has parameter receiver so you should use that to get the receiver name

I understand that I can do that, but why is is more correct to access it through ‘receiver’ rather than ‘email.receiver’?

Please open your own topic