I have tried methods suggested in the Forum. But I still get the message: 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()
def send_email(self, receiver, subject, body):
email = Email(sender=self, receiver=receiver, subject=subject, body=body)
receiver.inbox.receive_email(email)
# User Editable Region
print (f"Email sent from Alice to Bob!\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/138.0.0.0 Safari/537.36
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 receiver_name to sender_name!\\n")
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')
There are two ways you can format your code to make it easier to read and test:
After you copy/paste your code into the editor, select it by dragging your cursor over it then click the (</>) button in the toolbar to automatically wrap your code in backticks. (You can click on the animated demo image below to enlarge it.)
Manually add three backticks on a new line above your code and on a new line after your code. Note that a backtick is NOT the same as a single quote('). To find the backtick key on your keyboard, see this post.
To see changes to your post as you make them, you can click the (M+) button on the toolbar to bring up the rich text editor:
this does not resolve to contains the sender name and the receiver name, you need first to remember how to interpolate values in a string, and also how to access the two names from the values you have available inside send_email
Please create your own topic when you have specific questions about your own challenge code. Only respond to another thread when you want to provide help to the original poster of the other thread or have follow up questions concerning other replies given to the original poster.
The easiest way to create a topic for help with your own solution is to click the Help button located on each challenge. This will automatically import your code in a readable format and pull in the challenge URL while still allowing you to ask any question about the challenge or your code.