Plan your Trip works but doesn't work!


#1

Hi, I wrote this code, ran it, but nothing prints to console. Whats wrong? Thanks

def hotel_cost(nights):
    return 140 * nights
def plane_ride_cost(city):
    if city == "Charlotte":
        return 183
    elif city == "Tampa":
        return 220
    elif city == "Pittsburgh":
        return 222
    elif city == "Los Angeles":
        return 475
def rental_car_cost(days):
    cost = 40 * days
    if days >= 7:
        return cost - 50
    elif days >= 3:
        return cost - 20
    else:    
        return cost

    
def trip_cost(city, days, spending_money):
    return plane_ride_cost(city) + rental_car_cost(days) + hotel_cost(days) + spending_money

    print trip_cost("Los Angeles", 5, 600)

#2

Well firstly, you don't have a call to any function that does anything. Secondly please format your code properly.

How-to: Format your code

  1. Highlight selected code
  2. Press ctrl + alt + c or hit the </> key above the text entry area\

OR

```python

# Code here

``` # End

If you need any more assistance please feel free to ask.

Also you will know if your code is formatted correctly if it appears like

def foo(bar):
    return bar * 5

or

def foo(bar):
    return bar * 5

#3

Thanks!

I re-formatted the code as you suggested, will certainly do that from now on.

I'm certainly finding it challenging getting my head around functions. Could you elaborate
when you say "you don't have a call to any function that does anything"

How do I address/fix that?

Thanks again


#4

@ronnumber

All a function is, is a container that hold data and in the case of these function code as well. The data does nothing inherently so unless you tell the computer 'hey I want you to run code block 47' it will never do it unless you actually pass it the command to do so.

Example:

def a_function(data=None):
    if data == None:
        raise TypeError('No data entered')
    else:
        print('You passed this function some data: %s' % data)

Now notice that when you run this codebit it does absolutely nothing at all. That is because we have failed to call any function or run code that is not in a function.

Once we add a function call to our function it will execute our code.

a_function()
# OUTPUT: TypeError: No data entered

Ok, so we failed to give out function valid input, let's change it so that input is valid.

a_function('There is data here')
# OUTPUT: You passed this function some data: There is data here

It is easy to debug your code once you start, but remember that generally 80% of a coders time is spent debugging so do it right the first time. Take time to think about what you need done and go from there. That way you spend less time on debugging.

I also hope you realized by now that using functions is easy as pie, messing up functions is also easy.

So in closing, if you do not use your class/function or other encapsulated code it does nothing and just sits there.


#5

Thank you!

I figured from what you said that the final print trip_cost() command was accidentally inside the trip_cost function itself. I took it outside the function by eliminating the indent. After that, the function was called and it printed to the console.

Thanks again, you certainly helped me think about it in the right way.