Question: In this task, were going to be simulating an email message. Some of the logic has been filled out for you in the email.py file.
In this task, were going to be simulating an email message. Some of the logic has been filled out for you in the email.py file.
Open the file called email.py.
Create a class definition for an Email. The initialiser takes in two arguments and stores them as instance-level variables:
from_address - the senders email address. subject_line - the subject of the email.
email_contents - the content of the email.
In addition, the initialiser will create two more instance-level variables with default values:
has_been_read - initialised to False.
is_spam - initialised to False.
Create a function in this class called mark_as_read which should change has_been_read to true
. Create a function in this class called mark_as_spam which should change is_spam to true.
Create another class called Inbox to store all emails (note that you can have a list of objects). The initaliser doesnt take any arguments, and only initialises an empty list. This list is where all of your Email objects will be stored.
Within the Inbox class, create the following methods:
add_email(self, from_address, subject_line, email_contents) - which takes in the contents and email address from the received email to make a new Email object and store it in the inbox.
list_messages_from_sender(self, sender_address) - returns a string showing all subject lines in emails from a specific sender, along with a corresponding number.
For example, if there are three emails sent from a specific person::
0 Welcome to The World!
1 Great work !
2 Re: Your excellent marks
get_email(self, sender_address, index) - returns the email at a specific index from a specific user. In the example above, given the same sender_address, an index of 0 will return the email with the subject line Welcome to The World!.
Once that email has been returned, set its has_been_read instance variable to True.
mark_as_spam(self, sender_address, index) - Using the same indexing as above, mark the email at a specific index within a sender address as spam.
get_unread_emails(self) - should return a string containing a list of all the emails that havent been read. Only the subject lines need to be shown.
get_spam_emails(self) - should return a string containing a list of all the emails that have been marked as spam.
delete(self, sender_address, index) - deletes an email in the inbox. Now that you have these set up, lets get everything working!
Fill in the rest of the logic for what should happen when the user inputs read/mark spam/send/quit. Some of it has been done for you.
####Following the file email.py####
usage_message = '''
Welcome to the email system! What would you like to do?
s - send email.
l - list emails from a sender.
r - read email.
m - mark email as spam.
gu - get unread emails.
gs - get spam emails.
d - delete email.
e - exit this program.
'''
#An Email Simulation
user_choice = ""
while True:
user_choice = input(usage_message).strip().lower()
if user_choice == "s":
# Send an email (Create a new Email object)
sender_address = input("Please enter the address of the sender :")
subject_line = input("Please enter the subject line of the email :")
contents = input("Please enter the contents of the email :")
# Now add the email to the Inbox
# Print a success message
print("Email has been added to inbox.")
pass
elif user_choice == "l":
# List all emails from a sender_address
sender_address = input("Please enter the address of the sender :")
# Now list all emails from this sender
pass
elif user_choice == "r":
# Read an email
# Step 1: show emails from the sender
sender_address = input("Please enter the address of the sender of the email :")
# Step 2: show all emails from this sender (with indexes)
# Step 3: ask the user for the index of the email
email_index = int(input("Please enter the index of the email that you would like to read :"))
# Step 4: display the email
pass
elif user_choice == "m":
# Mark an email as spam
# Step 1: show emails from the sender
sender_address = input("Please enter the address of the sender of the email :")
# Step 2: show all emails from this sender (with indexes)
# Step 3: ask the user for the index of the email
email_index = int(input("Please enter the index of the email to be marked as spam :"))
# Step 4: mark the email as spam
# Step 5: print a success message
print("Email has been marked as spam")
pass
elif user_choice == "gu":
# List all unread emails
pass
elif user_choice == "gs":
# List all spam emails
pass
elif user_choice == "e":
print("Goodbye")
break
elif user_choice == "d":
# Delete an email
# Step 1: show emails from the sender
sender_address = input("Please enter the address of the sender of the email :")
# Step 2: show all emails from this sender (with indexes)
# Step 3: ask the user for the index of the email
email_index = int(input("Please enter the index of the email to be deleted :"))
# Step 4: delete the email
# Step 5: print a success message
print("Email has been deleted")
pass
else:
print("Oops - incorrect input")
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
