title: How do I catch NameError in Python?
body: <p>I've been trying to write a simple program in python to use classes and test attributes. It asks the user to input one of the names, and uses that input to display 2 attributes of the name. I've tried to include a try...except block to catch the NameError that occurs when typing something that hasn't been defined as the name of an object, but I still get this traceback:</p>
  
  <pre><code>Traceback (most recent call last):
  File "C:/Users/Bede/Documents/Technology/Programming/Python/276/testcode", line 18, in      &lt;module&gt;
  animalName = input("&gt;")
  File "&lt;string&gt;", line 1, in &lt;module&gt;
  NameError: name 'Marmadukke' is not defined
  </code></pre>
  
  <p>I'm aware this probably isn't the best way to do things, so I'm open to all suggestions.</p>
  
  <p>My full code is here:</p>
  
  <pre><code>class Dog(object):
      def __init__(self,name,chasesCats):
          self.name = name
          self.chasesCats = chasesCats
  
  class Cat(object):
      def __init__(self,name,chasesMice):
          self.name = name
          self.chasesMice = chasesMice
  
  Marmaduke = Cat("Marmaduke",True)
  Timmy = Cat("Timmy",False)
  Cerberus = Dog("Cerberus",True)
  Max = Dog("Max",False)
  
  print("Enter Marmaduke, Timmy, Max or Cerberus.")
  
  animalName = input("&gt;")
  while 1:
      try:
          if isinstance(animalName,Cat):
              print("Cat")
              if animalName.chasesMice:
                  print("Chases mice")
              else: print("Doesn't chase mice")
          if isinstance(animalName,Dog):
              print("Dog")
              if animalName.chasesCats:
                  print("Chases cats")
              else: print("Doesn't chase cats")
          break
      except NameError:
              print("Try again!")
  </code></pre>