0

I keep getting a NameError stating that CarInventory is not defined, as well as highest. Also, I am still not sure if my code for the method compare() is actually right.

This is my code for the assignment:

class CarInventory():
   def __init__(self, n_cars = 0, cars = []):
      self.n_cars = n_cars
      self.cars = cars
   def add_car(manufacturer,model,year,mpg):
      self.cars = {'manufacturer': self.manufacturer, 'model': self.model, 'year': self.year, 'mpg': self.mpg}
      self.n_cars = self.n_cars + 1
   def compare(self, attribute, direction=highest):
      self.lowest = self.cars[0]
      self.direction = self.cars[0]
        for car in self.cars:
           if self.car[attribute] < self.lowest:
              self.lowest = car
           elif self.car[attribute] > self.highest:
              self.highest = car
      if direction == highest:
        output = highest 
      elif direction == lowest:
        output = lowest
      return output
Woody1193
  • 7,252
  • 5
  • 40
  • 90
  • 1
    Does the indentation level in what you've posted match your code? – bphi Nov 26 '18 at 21:15
  • Sorry, I just fixed it. Copy&Paste mistake – Charlie C. Dang Nov 26 '18 at 21:19
  • What is `highest` supposed to be in `def compare(self, attribute, direction=highest):`? – user2357112 Nov 26 '18 at 21:19
  • You should be aware of the problems that come with a [Mutable Default Argument](https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument). It also sounds like your assignment expects you to have a `Car` class that your `CarInventory` contains some collection of. – Patrick Haugh Nov 26 '18 at 21:43

2 Answers2

1

The reason highest is giving you a NameError is that you have written:

def compare(self, attribute, direction=highest):

which tells Python that, if the caller does not include the second parameter (direction) when calling the compare method, the method should use whatever value is in the variable named highest. But no such variable is declared in your program.

You probably meant to write:

 def compare(self, attribute, direction='highest'):

which tells Python to use the literal string value of highest if no value is provided by the caller for the direction parameter.

As to the CarInventory error, you are not showing us the part of the program that is generating that error. It is located where ever you are trying to use the CarInventory class, not where you defined it.

Larry Lustig
  • 49,320
  • 14
  • 110
  • 160
  • Thank you so much. So to set the value for the parameter, it should always be a value such as '..', [...], (...) ? – Charlie C. Dang Nov 26 '18 at 22:00
  • You don't need to set *any* value for the `direction` parameter. If you do not, then the calling program (the part of the code you haven't shown us) is required to supply the value for every call. The default syntax you're using is a convenience if, for instance, the *most common* case is `'highest'` -- it allows the caller to leave out the value and get, by default, the specified value. Personally, I wouldn't use default parameter values until you have more programming experience. – Larry Lustig Nov 26 '18 at 22:08
0

I can see a number of problems with your code.

First, I noticed a lot of references to self.X where X was not a member of CarInventory. This is a misuse of self, which is used to scope a variable to the class (or occasionally function) the interpreter should look in. If you are referencing self.manufacturer and CarInventory doesn't have a member called manufacturer then you'll get an exception.

I also noticed that in your add_car function, you're setting the entire list of cars to the new car you're adding. Given the definition of this method, I don't think that's what you want. Instead, you should use the append method to add the new car to your list of cars.

There were a number of problems with compare as well. For one, you were trying to apss in some variable called highest rather than the string 'highest' for the argument for direction, which is where your NameError came from. You also had a number of comparison issues and a missing else.

I have fixed your code

class CarInventory():

    def __init__(self, n_cars = 0, cars = []):
        self.n_cars = n_cars
        self.cars = cars

    def add_car(manufacturer,model,year,mpg):
        self.cars.append({
            'manufacturer': manufacturer, 
            'model': model, 
            'year': year, 
            'mpg': mpg})
        self.n_cars = self.n_cars + 1

    def compare(self, attribute, direction = 'highest'):
        lowest = self.cars[0]
        highest = self.cars[0]
        for car in self.cars:
            if car[attribute] < lowest[attribute]:
                lowest = car
            elif car[attribute] > highest[attribute]:
                highest = car
       if direction == 'highest':
           return highest 
      else:
          return lowest

As an aside, you should put newlines between your class definitions and functions for readability.

Woody1193
  • 7,252
  • 5
  • 40
  • 90
  • Hello, thank you so much for answering and helping. I understand the concept of dictionary better now. However, how do I get a value from a key of a dictionary? I still get a TypeError saying "TypeError: '<' not supported between instances of 'int' and 'dict'". – Charlie C. Dang Nov 26 '18 at 22:21
  • That's because `lowest` and `highest` are `dict`s but `car[attribute]` is, in your case, an `int`. I have updated my answer to reflect the changes – Woody1193 Nov 27 '18 at 16:58