Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Quote:
Aspects of the submission that could be improved
Getting there, here are a few things to have a look at:
* The email and the content are mixed up - a way forward in order not to mix up data is to be consistent in your code, if you are not sure what comes first, you can always go back and look. consider add_email on line 76.
* delete is a bit buggy, the indexes shown and the one that is deleted is not the same as the one minus the number chosen is delete, kindly refer back to the delete section and check if there aren't any calculations that might be causing this.
* Currently, the code is when run is stuck on the init_emails and changes are not applied as the code functionality uses a different list, I suggest using the same list, throughout so that changes are stored.


What I have tried:

# 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")
Posted
Updated 1-Aug-22 21:23pm
Comments
Richard MacCutchan 2-Aug-22 3:38am    
No one here has seen this code before so their chances of understanding what is wrong are not high. You need to explain exactly where these "improvements" need to be implemented.

1 solution

OK, so it runs now. That's not the end of your task!

Getting it to run does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
Comments
Thandeka Zulu 2-Aug-22 4:15am    
Thank you
OriginalGriff 2-Aug-22 4:18am    
You're welcome!

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



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