Build an Email Simulator - Step 43

Tell us what’s happening:

What is the problem in that code? im still getting error"You should print the formatted timestamp using self.timestamp.strftime(‘%Y-%m-%d %H:%M’)"
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(self.timestamp.strftime(‘Received: %Y-%m-%d %H:%M’))
print(f’Body: {self.body}’)
print(‘------------\n’)

Your code so far


# User Editable Region

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(self.timestamp.strftime('Received: %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}"
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)

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


# User Editable Region

Your browser information:

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

Challenge Information:

Build an Email Simulator - Step 43

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/workshop-email-simulator/6853f70470f45d9c280af659.md at main · freeCodeCamp/freeCodeCamp · GitHub

Welcome back to the forum @vuthnarak2005

You should print the formatted timestamp using self.timestamp.strftime('%Y-%m-%d %H:%M'). Add Received: followed by a space before it.

The string Received: needs to go before the timestamp.

Happy coding

Is that currect sir ?print(‘Received:’, self.timestamp.strftime(’ %Y-%m-%d %H:%M’))

Hi @vuthnarak2005

Use an f-string.

Happy coding

Even trying with a f-string

print(f'Received: {self.timestamp.strftime('%Y-%m-%d %H:%M')}')

I get the following error :

Traceback (most recent call last):
File “main.py”, line 21
print(f’Received: {self.timestamp.strftime(‘%Y-%m-%d %H:%M’)}')
^
SyntaxError: f-string: unmatched ‘(’

Skipping to the next step and checking the code indicates the answer is correct. Even importing the code in an external IDE (namely Pycharm in my case) doesn’t result in this error.

hi @ecart , welcome to the forum, please 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.