Click here to Skip to main content
15,884,739 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Compulsory Task 1
In this task, we’re 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 which has four variables:
has_been_read, email_contents, is_spam and from_address.
● The constructor should initialise the sender’s email address.
● The constructor should also initialise has_been_read and is_spam 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 a list called inbox to store all emails (note that you can have a list of
objects).
● Then create the following methods:
○ add_email - which takes in the contents and email address from the
received email to make a new Email object.
○ get_count - returns the number of messages in the store.
○ get_email - returns the contents of an email in the list. For this, allow
the user to input an index i.e. get_email(i) returns the email stored
at position i in the list. Once this has been done, has_been_read
should now be true.
○ get_unread_emails - should return a list of all the emails that haven’t
been read.
○ get_spam_emails - should return a list of all the emails that have been
marked as spam.
○ delete - deletes an email in the inbox.
Now that you have these set up, let’s get everything working!
● Fill in the rest of the logic for what should happen when the user inputs
send/read/quit. Some of it has been done for you


What I have tried:

class Email:
    has_been_read = False
    self_is_spam = False
    
    def __init__(self, email_contents, from_address):
        self.from_address = from_address
        self.email_contents = email_contents
        
    def mark_as_read(self):
         self.has_been_read = True
        
    def mark_as_spam(self):
        self.is_spam = True
        
inbox = []
      
def add_email(Email, email_contents, from_address):
    add_email("OOP leve1", "python@hyperion.co.za")
    
def get_count(inbox):
    return len(inbox)
    
    
def get_email(inbox,index_email):
    inbox[index_email].mark_as_read()
    return inbox[index_email].email_contents
        
def get_unread_email (inbox):
    unread_inbox = []
    for email in inbox:
        if email.has_been_read == False:
            unread_inbox.append(email)
        
def get_spam_emails (inbox):
    spam_list = []
    for email in inbox:
        if email.mark_as_spam():
            spam_list.append(email)

def delete (inbox, index_email):
    return inbox.pop(index_email)
        
userChoice = ""    
while user_choice != "quit":
    user_choice = input("What would you like to do - read/mark spam/send/quit?").lower()
    if user_choice == "read":
        print(int(input("Enter index of email you want to read: ")) 
        print(get_email(inbox, index_email))
    elif user_choice == "mark spam":
        print(get_spam_emails(inbox))
    elif user_choice == "send":
        from_address = input("Enter your email address: ")
        email_contents = input("Enter email content: ")
        print(add_email(Email, email_contents, from_address))
    elif user_choice == "quit":
        print("Goodbye")
    else:
        print("Oops - incorrect input")
Posted
Updated 28-Jul-22 0:13am

You have the following errors, all of which Python tells you:
At line 43:
Python
userChoice = ""    

But all the following lines spell it user_choice.

At line 47:
Python
print(int(input("Enter index of email you want to read: "))

I think you mean something like:
Python
index = int(input("Enter index of email you want to read: "))


At Line 54:
Python
print(add_email(Email, email_contents, from_address))

But Email at this point has no meaning at this point. I suggest you take a look at 9. Classes — Python 3.10.5 documentation[^].

There are probably other errors which will show up in your testing.
 
Share this answer
 
Comments
Thandeka Zulu 28-Jul-22 8:58am    
Thanks for that documentation. Will make the changes, then get back to you
Richard MacCutchan 28-Jul-22 9:39am    
You need to examine each error message as it appears, and work out what needs to be changed to correct it. If you are not sure then make use of the Python documentation.
To add to what Richard has - rightly - said: You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a syntax error!
 
Share this answer
 
Comments
Thandeka Zulu 28-Jul-22 9:00am    
Will definately look into that. Thank you.
Thandeka Zulu 2-Aug-22 2:47am    
# This program simulates an email message
class Email:

# This constructor initialises senders email address,and has four variables
def __init__(self,from_address, email_contents):
self.from_address = from_address
self.email_contents = email_contents
self.has_been_read = False
self.is_spam = False

# This function changes "has_been_read" variable from false to true
def mark_as_read(self):
self.has_been_read = True

# This function allows us to print the email, especially when the user want to read
def __str__(self):
return f"From: {self.from_address} \nContent: {self.email_contents} "

# This function changes "is_spam" variable from false to true
def mark_as_spam(self):
self.is_spam = True

# List is created to store emaildetails
inbox = []

# Number of messages in the store are returned via this function
def get_count(inbox):
return len(inbox)

# Email content is returned by this function
def get_email(index):
message = inbox[index]
message.mark_as_read()
print(message)

# Non read emails from the list are returned via this function
def get_unread_emails():
messages =[]
for n, i in enumerate(inbox):
if not i.has_been_read:
message = inbox[n]
messages.append(message)
print(message.email_contents)
return messages

# This function returns a list of all the emails that have been marked as a spam
def get_spam_emails():
print()
messages =[]
for n, i in enumerate(inbox):
if not i.is_spam:
message = inbox[n]
messages.append(message)
print(f"spam: {message.email_contents}")
return messages

def add_spam(index):
messages = inbox[index]
messages.mark_as_spam()
print("Email added to spam")



# Delete function deletes an email in the inbox
def delete(index):
try:
inbox.remove(inbox[index])
print("Email deleted successfully")
except:
print("Error: couldn't delete email")

# This variable store user input, the choice they choose from the menu
user_choice = ""

inbox = []
def add_email(message, email):
send = Email(message, email)
inbox.append(send)


# Initial email
init_emails = [
'Hie how are you,alfred@gmail.com',
'Hie lets meet us,thuba@gmail.com'
]

for i in init_emails:
message, email = i.split(',')
add_email(message,email)

#This is the menu the user will be prompted with to enter choice
while user_choice != "quit":
user_choice = input("What would you like to do - read/mark spam/send/delete/quit?\n").lower()

if user_choice == "read":
print("List of unread email\n")
for i,mail in enumerate(init_emails):
print(f'{i+1}. {mail}')
num = int(input("\nEnter number of email you want to read: "))
get_email(num-1)
#num = int(input("Enter number of email you want to read: "))
#get_email(num-1)
elif user_choice == "mark spam":
print("List of emails\n")
for i,mail in enumerate(init_emails):
print(f'{i}. {mail}')
num = int(input("\nEnter number of email you want to spam :"))
add_spam(num-1)
get_spam_emails()
#add_spam(num-1)
#get_spam_emails()
elif user_choice == "send":
from_address = input("Enter your email address: ")
email_contents = input("Enter email content: ")
add_email(email_contents, from_address)
print("Email sent successfully!")
elif user_choice == "delete":
print("List of emails\n")
for i,mail in enumerate(init_emails):
print(f'{i}. {mail}')
num = int(input("enter number of email to delete"))
delete(num-1)
#num = int(input("enter number of email to delete"))
#delete(num-1)
elif user_choice == "quit":
print("Goodbye")
break
# else alet user that the entry is invalid
else:
print("Oops - incorrect input")

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900