homework confusion

Lauren Fugate lauren.sophia1998 at gmail.com
Wed Apr 12 22:13:19 EDT 2017


Hello! So I have a homework assignment that I can't seem to understand. The problems are talking about classes and subclasses. My issue is that the examples use objects as arguments and I don't know how to make that happen without getting a name error. The question is:

Create a subclass of Openable called Lockable for representing physical objects that can be unlocked and opened. Remember to make sure that Lockable is a subclass of Openable. In addition to the methods and attributes it automatically inherits from Openable and Thing, it should have:
A public boolean attribute called is_locked that represents whether or not the object is locked.
A public attribute called key which is a Thing object that represents the item that can be used as akey to unlock the Lockable object.
An overridden constructor that takes two strings (the name and the location), a Thing (the key), and two optional boolean values (first, whether the object starts off open and second, whether it starts out locked). If neither of the optional boolean values are given, then the object should default to being closed but not locked. If only the first of the optional arguments (the starting "openness" state) is given, then the object should start out unlocked.
Your version of .__init__() for Lockable must use the version of .__init__() from the superclass Openable to handle the name, location, and "openness" state. Setting the is_locked and key attributes are the only ones you need to handle separately.
So you should be able to create Lockable objects with commands* like Lockable("front door", "in the foyer", house_key, False, True) (returning an object that starts out closed and locked), or Lockable("diary", "under Sam's bed", tiny_key, True) (returning an object that starts out open and unlocked), or Lockable("ancient treasure chest", "at the bottom of the sea", rusty_key) (returning an object that starts out closed and unlocked).
*Assuming that the variables house_key, tiny_key, and rusty_key already exist and refer to instances of Things.

My code is:
class Thing:
    """represents physical objects in a game

    attributes: name(str), location(str)"""
    def __init__(self, name, location):
        """creates a Thing with a name and location

        str, str"""
        self.name = name
        self.location = location
    def __repr__(self):
        """returns the Thing as a string that could be used to create a Thing

        None-> str"""
        return "Thing(" + self.name + ", " + self.location + ")"
    def description(self):
        """returnd the Thing along with its name and location in a string

        None -> str"""
        return "The " + repr(self.name) + " is " + repr(self.location) + "."
    
class Openable(Thing):
    """representing the Things that can be opened

    attributes: name(str), location(str), is_open(str)"""
    def __init__(self, name, location, is_open = False):
        """creates a Thing with a name and location

        str, str"""
        Thing.__init__(self, name, location)
        self.is_open = is_open
    def __repr__(self):
        """returns the Thing as a string that could be used to create an Openable

        None-> str"""
        return "Openable('" + self.name + "', '" + self.location + "', '" + str(self.is_open) + "')"
    def description(self):
        """returnd the Thing along with its name and location and whether it is open in a string

        None -> str"""
        if self.is_open == True:
            opened = "is open"
        else:
            opened = "is closed"
        return "The " + self.name + self.location + " is " + opened + "."
    def try_open(self):
        """tries to open the Openable and returns the result

        None -> bool"""
        if self.is_open == True:
            return False
        if self.is_open == False:
            self.is_open = True
            return True

class Lockable(Openable):
    """respresents the Openable Things that are Lockable

    attributes: name(str), location(str), is_open(str), is_locked(str), key(str)"""
    def __init__(self, name, location, key, is_open = False, is_locked = False):
        """creates a Thing with a name and location

        str, str"""
        Openable.__init__(self, name, location, is_open = False)
        self.is_locked = is_locked
    def __repr__(self):
        """returns the Thing as a string that could be used to create an Lockable

        None-> str"""
        return "Lockable('" + self.name + "', '" + self.location + "', '" + str(self.is_open) + "', '" + str(self.is_locked) + "')"
    def description(self):
        """returnd the Thing along with its name and location and whether it is open in a string and whether it is locked

        None -> str"""
        if self.is_open == True:
            opened = "is open"
            return "The " + self.name + self.location + " is " + opened + "."
        elif self.is_open == Flase:
            opened = "is closed"
            if self.is_locked == True:
                locked = "is locked"
                return "The " + self.name + self.location + " is " + locked + "."
            else:
                locked = "is unlocked"
        return "The " + self.name + self.location + " is " + opened + " but " + locked + "."

The example command is: Lockable("diary", "under Sam's bed", tiny_key, True)

And I keep getting a NameError: tiny_key is not defined. 

What do I do?



More information about the Python-list mailing list