Python Forum
NameError: name 'mailbox_list' is not defined - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: NameError: name 'mailbox_list' is not defined (/thread-4281.html)



NameError: name 'mailbox_list' is not defined - pythonnewb - Aug-04-2017

I have 2 classes imported onto my demo file but when I try to print my list a name error pops up, when I already defined the empty list 'mailbox_list' on line 33. What am I doing wrong?

from message import Message
from mailbox import Mailbox

# create objects from Message class
msg1=Message('Kim', 'Samuel', 'Please depart promptly.')
msg2=Message('Susan', 'Annie', 'Pick up your things')
msg3=Message('Rudolf Reindeer', 'Frosty Snowman', 'Christmas is coming!')
msg4=Message('Bob', 'Tom', 'Bob the Builder reruns')

# append additional lines
msg1.append("You don't want to be late")
msg2.append('You left your backpack and textbooks')
msg3.append('I am very excited.')
msg4.append('are very entertaining')

# create message strings
mail1=msg1.toString(msg1.sender,msg1.recipient,msg1.message)
mail2=msg2.toString(msg2.sender,msg1.recipient,msg2.message)
mail3=msg3.toString(msg3.sender,msg3.recipient,msg3.message)
mail4=msg4.toString(msg4.sender,msg4.recipient,msg4.message)

# create object from Mailbox class
inbox=Mailbox()

# inbox as list to store messages
mailbox_list=[]

# use addMessage method to add to list
inbox.addMessage(mail1)
inbox.addMessage(mail2)
inbox.addMessage(mail3)
inbox.addMessage(mail4)

# remove messages
inbox.removeMessage(mailbox_list[0])

# display emails 
for item in mailbox_list:
    print(item+'\n')
The following is the Mailbox class that was imported
from message import Message

# Mailbox class
class Mailbox:

    # create messages list
    mailbox_list=[]
        
    # method that adds message
    def addMessage(self, message):
        mailbox_list.append(message)

    # method that gets message
    def getMessage(self, index):
        mailbox_list[index].toString()

    # method that deletes messages
    def removeMessage(self,message):
        mailbox_list.remove(message)

Here is the Message class
class Message:

    # initialze attirubtes
    def __init__(self, sender, recipient, message):
        self.sender = sender
        self.recipient = recipient
        self.message = message

    # set sender and recipient
    def set_sender(self,sender):
        self.sender = sender
    def set_recipient(self, recipient):
        self.recipient = recipient
        
    # method that appends text to message
    def append(self, text):
        text = '\n' + text
        self.message += text
        return self.message

    # method that strings entire message
    def toString(self, sender, recipient, message):
        self.msgStr='From: ' + self.sender + '\nTo: ' + self.recipient + '\n' + self.message
        return self.msgStr

I made some modifications that allows the program to execute, however, I need to be using the objects created from the 2 classes. It only executes if I don't use the objects. The multilne comments are what I originally had (uses the object to modify list).
from message import Message
from mailbox import Mailbox

# create objects from Message class
msg1=Message('Kim', 'Samuel', 'Please depart promptly.')
msg2=Message('Susan', 'Annie', 'Pick up your things')
msg3=Message('Rudolf Reindeer', 'Frosty Snowman', 'Christmas is coming!')
msg4=Message('Bob', 'Tom', 'Bob the Builder reruns')

# append additional lines
msg1.append("You don't want to be late")
msg2.append('You left your backpack and textbooks')
msg3.append('I am very excited.')
msg4.append('are very entertaining')

# create message strings
mail1=msg1.toString(msg1.sender,msg1.recipient,msg1.message)
mail2=msg2.toString(msg2.sender,msg1.recipient,msg2.message)
mail3=msg3.toString(msg3.sender,msg3.recipient,msg3.message)
mail4=msg4.toString(msg4.sender,msg4.recipient,msg4.message)

# create object from Mailbox class
inbox=Mailbox()

# inbox as list to store messages
# WITHOUT OBJECT USE
mailbox_list=[mail1,mail2,mail3,mail4]

'''# use addMessage method to add to list
inbox.addMessage(mail1)
inbox.addMessage(mail2)
inbox.addMessage(mail3)
inbox.addMessage(mail4)'''

'''# remove messages
inbox.removeMessage(mailbox_list[0])'''
# WITHOUT OBJECT USE
mailbox_list.remove(mailbox_list[0])

# display emails 
for item in mailbox_list:
    print(item+'\n')



RE: NameError: name 'mailbox_list' is not defined - nilamo - Aug-05-2017

Quote:
# inbox as list to store messages[/code]
mailbox_list = []

### irrelevant stuff removed

# remove messages
inbox.removeMessage(mailbox_list[0])
 

So you're defining a new list, and then immediately trying to access the first item in that list.  But you never added anything to the list, so there is no first item.  Index 0 doesn't exist, so trying to access the item at that index is an error.


RE: NameError: name 'mailbox_list' is not defined - pythonnewb - Aug-06-2017

Thank you for your answer. Doesn't the addMessage method add items to the defined list?