Plan your trip! Nothing printed to console


#1

I can not get my program to print. I have checked the indentation for trip_cost and believe it is correct. Please advise.


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
    else:
        return "enter valid destination"
def rental_car_cost(days):
    tot = 40 * days
    if days >= 7:
        return tot - 50
    elif days >= 3 and days < 7:
        return tot - 20
    else:
        return tot
        
def trip_cost(city, days, spending_money):     
    return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city) + spending_money
    
    print trip_cost('Los Angeles', 5, 600)


#2

Your code defines four functions and then it's done, it does nothing else. Why would it print?

Python starts from the top and executes each statement. You have four statements, each one a function. Each function statement has the effect of creating those functions. There's nothing other than that in your code. Control flow would need to reach a print statement to print something. What control flow do you imagine your code having? What would the code have to look like to match that?


#3

I thought the fourth function "returns" the sum of previous three plus the spending money and that becomes the total_cost which I then want to print.

my final call copied the Hint.

I apologize as I must be missing something very basic.


#4

It's not gonna return if it doesn't run. Your code defines it. That's it, nothing else happens. It does not execute. To execute a function you will need to call it.

As said previously, when Python runs your code, it creates four function and after that it reaches the end of the file.

This code does not print anything:

def f():
    print 'hello world'

It only creates a function. That function is never called.


#5

I understand what you are saying and have tried to create a fifth function to print the answer. However, I am getting the same message. I'm missing something basic. I need to see examples of the format that works. Can you direct me? Does Codeacademy have a reference guide illustrating the building blocks within Python?


#6

In order to execute my function above, you would add a call to it:

def f():
    print 'hello world'

f() # call the function, 'hello world' is printed

#7

def trip_cost(city, days, spending_money):

return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city) + spending_money

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

This is how I am trying to call the function, but it is not working.


#8

Your formatting is messed up there. But if it looks this way:

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

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

Then see that there is a function and it gets created, after that the end of file is reached.

What you need to do is create a function, and then call it.

# create function
def f():
    print 'hello world'

# then call it
f()

You're not calling it, you've only created it. You're missing the line that calls the function, so all code that is in the function will remain un-executed.

Now if you were to claim that you do have a line that will execute and call the function, then note that I'm saying that you have no such line, that there is only the function - then would that not be saying that you are calling your function from within your function? Well that's not going to happen is it, because the function is never called in the first place. You have to call the function before anything inside it will run.

In the below example, nothing is printed, because the function f is created, and then the end of file is reached, the program terminates.

# create function
def f():
    print 'hello world'

    # this is inside the function, it will never execute, because
    # the function is never invoked
    f()

#9

1) create a function
2) invoke that function

I keep telling you that you're only doing 1), that means that whatever you think is doing 2) isn't doing 2). Program flow simply does not pass the location where you are doing 2) so it should then be evident that you must put 2) where program flow does pass. If program flow goes from top to bottom, then you should start by creating the function, and then you follow by calling it, that means the function call must come after, not within, the function. So where does the function stop and when is "after" the function?


#10

Thank you. I appreciate you bearing with me and sorry I didn't get the concept on your first response.

Interestingly, the system congratulated me and said I finished the course, but the console print

None

I'm going to proceed and come back when I have more time to get the actual number answer.


#11

sorry console printed

function trip_cost at 0x7fee4c73eb90
None


#12

That's what you get if you print a function. Is that really what you wanted to do? Usually what one would do is invoke the function and print the result, not the function itself.

As for getting green light without printing anything:

what you're supposed to do,
what you think you're supposed to do,
what you're tested for having done

May all differ. Focus on carefully ensuring that the first two align and making that happen. It's mapping your intentions to code that matters, not what it accepts.


#13

Yes, I will adjust code to print answer. I want to align and understand it all.

917-825-7955


#14

You just need to indent you code otherwise all were ok like below
def trip_cost(city, days,spending_money):
return rental_car_cost(days) + plane_ride_cost(city) + hotel_cost(days)+spending_money
print trip_cost("Los Angeles", 5, 600)


#15

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