Learn Python 3 The Nile (https://www.codecademy.com/courses/learn-python-3/projects/function-args-the-nile)

For the exercise The Nile (https://www.codecademy.com/courses/learn-python-3/projects/function-args-the-nile), I’m trying to figure out how to test the function calculate_driver_cost by calling it with my own arguments.

The function is defined as:

def calculate_driver_cost(distance, *drivers):
  cheapest_driver = None
  cheapest_driver_price = None
  for driver in drivers:
    driver_time = driver.speed * distance
    price_for_driver = driver.salary * driver_time 
    if cheapest_driver is None:
      cheapest_driver = driver
      cheapest_driver_price = price_for_driver
    elif price_for_driver < cheapest_driver_price:
      cheapest_driver = driver
      cheapest_driver_price = price_for_driver
  return cheapest_driver_price, cheapest_driver

To call the function, I first created a Driver class and then created 2 drivers (not sure if I’m doing the right thing)

class Driver:
  def __init__(self, speed, salary):
    self.speed = speed
    self.salary = salary

driver1 = Driver(100 ,5)
driver2 = Driver(50, 100)

print(calculate_driver_cost(8, driver1, driver2))

This is what is returned: (4000, <main.Driver object at 0x7fbfcd086c88>)
How do I return the cheapest driver instead of the location code thing?? And is this even the way to go about calling the function.

Thanks in advance,

Tim

Have you inspected the test case? Why or why not?

1 Like

Hi. I actually did inspect the test case but I didn’t write in what else I tried.

From the test case, adding this to the driver class made it work:

def repr(self):
return “Nile Driver speed {} salary {}”.format(self.speed, self.salary)

I tried writing this initially:

def repr(self):
return self.speed, self.salary

But that didn’t work. I guess my question more specifically is why doesn’t just returning self.speed and self.salary make it work.

you can only return one thing. This one thing can of course be wrapped in a container (like a list or a tuple). However, a specific method (like __repr__) might have a certain signature. Have you checked what data types are allowed to be returned by __repr__?

self is only used inside the class. Once you have instantiated a class, we can access the instance variable like so for example:

driver.speed

Do you understand what __repr__ does?

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.