Python Forum
NameError: name 'mailbox_list' is not defined
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError: name 'mailbox_list' is not defined
#1
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')
Reply
#2
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.
Reply
#3
Thank you for your answer. Doesn't the addMessage method add items to the defined list?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation NameError: name 'score' is not defined - Help? MrKnd94 13 4,600 Mar-06-2023, 10:54 PM
Last Post: deanhystad
  How to correct the NameError: name 'xx' is not defined? vokoyo 5 11,323 Feb-17-2021, 05:55 AM
Last Post: delonbest
  NameError: name 'os' is not defined, & load_files(sys.argv[1]) AryaIC 3 4,720 Nov-07-2020, 07:45 PM
Last Post: jefsummers
  Error in code NameError: name ' ' is not defined ppman00 11 6,271 Sep-18-2020, 05:22 AM
Last Post: ndc85430
  NameError: name 'print_string' is not defined jamie_01 2 2,042 Jun-11-2020, 05:27 AM
Last Post: buran
  "NameError: name 'catName1' is not defined tofif 3 5,665 Jun-24-2019, 06:05 AM
Last Post: perfringo
  NameError x not defined Bruizeh 5 5,291 Feb-27-2019, 10:59 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020