Strange output?


#1




When I run the code, if I run Los Angeles, it shows that I get (995, 0) instead of 995 and I dont know why the parenthesis are there.


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):
    rental_car_cost = 40*days
    if days >= 7:
        rental_car_cost -= 50
    elif days >= 3:
        rental_car_cost -= 20
    return rental_car_cost
def trip_cost(city, days, spending_money):
    return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city), + spending_money


#2

this line:

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

the , in this line will convert the integer into a tuple with two integers, remove the ,


#3

It worked, thanks! I'm also trying to run the same line of code in CodeRunner, but the problem is nothing appears. I run the exact same line, but nothing is happening. Any thoughts as to what the problem could be?


#4

function don't execute unless you call them. In the codecademy lesson, the validation/checking script executes function calls, in coderunner this is not happening, you have to call the functions yourself


#5

So to gain information on
def trip_cost(city, days, spending_money):
return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city), + spending_money
I would call
def_cost("Los Angeles", 3, 0)

Like this?


#6

no, the function is called trip_cost, not def_cost.

also, if you want to print the returned result add print on the function call:

print trip_cost("Los Angeles", 3, 0)

#7

Yup I just realized i was thinking about the def function, it is trip_cost. My question is why do you have to write print? I thought when we had def we could just write trip_cost(enter all info here) and it would return the answer? I ran what you typed through CodeRunner and it came back with the correct results.


#8

Also, are you familiar with Atom? I want to use that over CodeRunner, but the problem is whenever I try to run raw_input there's always an error... Any way around this if you know?


#9

return means literal that, the function returns something, it hands you something back:

def example():
    return "hello world"
store = example()

the function returns something, this we can store in a variable, and then print it:

print store

or print it directly by adding a print statement to the function call

this has nothing to do with atom, codecademy runs python2.7.3, and you run very likely python3

python3 is not backward compatible with python2, raw_input is input in python3, and print also requires parentheses in python3 because it is now a function call rather then a statement:

print("hello world")

#10

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