Python 3: Function Argument (The Nile Project), problem with step 8

Hello, I am struggling with the Nile Project. when I test my code on step 8 using
test_function(calculate_shipping_cost)
It shows me the following:
calculate_shipping_cost() did not provide default argument for shipping_type

Here is my code:
def calculate_shipping_cost(from_coords, to_coords, shipping_type = ‘Overnight’):
from_lat, from_long = calculate_shipping_cost(from_coords)
to_lat, to_long = calculate_shipping_cost(to_coords)
distance = get_distance(*from_coords, *to_coords)
shipping_rate = SHIPPING_PRICES[shipping_type]
price = distance * shipping_rate
return format_price(price)

I am unsure what the issue is…

Best,
Gaetan.

When you invoke a function with default arguments, you need to also provide the arguments that are not default.

How did you invoke it? Can you copy/paste the compiler error?

Also, it helps to format so it’s clearer what the error could be
:slight_smile:: [How to] Format code in posts - #7

Hi,

Here is how I invoked the arguments:

def calculate_shipping_cost(from_coords, to_coords,  shipping_type = 'Overnight'):
  from_lat, from_long = calculate_shipping_cost(from_coords)
  to_lat, to_long = calculate_shipping_cost(to_coords)  
  distance = get_distance(*from_coords, *to_coords)
  shipping_rate = SHIPPING_PRICES[shipping_type]
  price = distance * shipping_rate
  return format_price(price)

test_function(calculate_shipping_cost)

it shows me the following error: calculate_shipping_cost() did not provide default argument for shipping_type

Apologies for not understanding, but can you explain what you mean by providing arguments that are not default?
Do you mean what the arguments are for shipping_type? From my understanding, these are invoked from the SHIPPING_PRICES dictionary from nile.py.

Thank you.

This declaration: def calculate_shipping_cost(from_coords, to_coords, shipping_type = 'Overnight'): means when it’s invoked, the function will have to be in the form of

calculate_shipping_cost(first_arg, second_arg, optional_third_arg)

The issue is really on what the tester function that Codecademy provides expects.
Sometimes it’s slightly off or sometimes it is really strict.

Since you seem to understand the basics of how to use default arguments, I would just copy your code, then compare it to the solution it provides for that. (This is after testing the function yourself to make sure it does work). The point is to learn programming, not the quirks of the tester.

Normally if you define a function with default parameters, you could invoke two ways (specifying the default, or leaving it alone):

def double_scale(x,y, z=0):
    return (2 * x, 2* y, 2 * z)

double_scale(4,5) #returns (8,10,0)
double_scale(1,2,3) #returns (2,4,6)

Thanks,

Unfortunately this exercise only provides a walkthrough video in which the code proposed to pass this step is the same as the code that I have sent in the previous email.
However, I have sort of gone in a different track and wrote this instead:

def calculate_shipping_cost(from_coords, to_coords,  shipping_type = None):
  if shipping_type == None: 
    return str(SHIPPING_PRICES["Overnight"])
  from_lat, from_long = calculate_shipping_cost(from_coords)
  to_lat, to_long = calculate_shipping_cost(to_coords)  
  distance = get_distance(*from_coords, *to_coords)
  shipping_rate = SHIPPING_PRICES[shipping_type]
  price = distance * shipping_rate
  return format_price(price)

test_function(calculate_shipping_cost)

and it gives me a different type of error:
calculate_shipping_cost((0, 0), (1, 1)) returned 2.3. Expected result is $1.04

I have to say that I am quite confused, as the “Overnight” rates are 2.3, so technically that should have been the expected value.
I realized I could brute force my way through by modifying the test_py values of $1.4 to 2.3 and it will the following:
OK! calculate_shipping_cost() passes tests

Although I am keen to progress this exercise, this is not a very elegant way to do so. Happy to hear more from others.

I went over and started the project to see what’s up, I think your problem lies in the second and third line of this code snippet. You’re asked to do this:

Both from_coords and to_coords are tuples, containing first the latitude and then the longitude. Since our get_distance() function looks for all four as separate arguments, we’ll need to separate these variables out.

Inside calculate_shipping_cost unpack those tuples into from_lat , from_long , to_lat , and to_long .

From what I understand, you’re trying to recursively call the function with missing arguments when declaring variables from_lat, from_long and all you’re supposed to do is separate from_coords into from_lat, from_long (same with to_coords)

Everything else from your first post was the same as what I got, and it passed the step 8 check.

2 Likes

Thanks for your help, it works!