Hey, You Never Know! of Taking a vacation exercise of functions


#1

Hello Friends,
I am into Taking into Vacation of Hey,You Never Know exercise of Python. I have written the code and executed
Each time i run the program, it prints the totaltripcostis1740 . Along with that it says Oops, try again. trip_cost('Tampa', 7, 3) returned 1430 instead of the correct value 1433. When i ran the program second time it prints
*totaltripcostis1740**, along with that it says Oops, try again. trip_cost('Los Angeles', 0, 69) returned 475 instead of the correct value 544
third time it prints the totaltripcostis1740 and along with that it says Oops, try again. trip_cost('Pittsburgh', 8, 47) returned 1612 instead of the correct value 1659
I could not figure it out where I have gone wrong. I request all folks to look into it and help me out.
regards
sam

def hotel_cost(nights):
costpernight = 140
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):
totalrent = days * 40
if days >=7:
totalrent -= 50
return totalrent
elif days>=3:
totalrent -= 20
return totalrent
return totalrent

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

total_trip_cost = trip_cost("Tampa",3,1000) + 1000
print "the totaltripcostis" "%d" %(total_trip_cost)


#2
def hotel_cost(nights):
    costpernight = 140 # not used, remove this
    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):
    totalrent = days * 40
    if days >=7:
        totalrent -= 50
        return totalrent
    elif days>=3:
        totalrent -= 20
        return totalrent 
    return totalrent

def trip_cost(city,days,spending_money):
    return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city) # add the missing 'spending_money' variable
    total_trip_cost = trip_cost("Tampa",3,1000) + 1000
    # I don't exactly what you mean with this code
    # but I think you should remove it
    print "the totaltripcostis" "%d" %(total_trip_cost)
    # Printing the value of 'total_trip_cost' is not mentioned in the instructions.

If there are some things that you think I've mis-indented or misunderstood in the code, feel free to say it.


#3

Thaks for the suggestions. I removed the extra lines of code that caused the problem and it worked . Moreover, can i write a write a function of spendingmone() instead of directly adding the variable though it's bit long way round than directly adding to the return statment.Please look at the below code

def trip_cost(city,days,spendingmoney):
def spendingmone(money):
return money
return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city) + spendingmoney(money)
trip_cost("Tampa",3,1000)


#4

Welcome.

But why would you need to create a function for 'spending_money' for this? The instructions say that it has to be a variable exactly named 'spending_money' (notice the underscore). Actually, at run-time, its value is given by the exercise itself as a test-case.

def trip_cost(city,days,spendingmoney):
# 'spendingmoney' should be changed to 'spending_money', exactly

I don't think making a function for 'spending_time' makes sense since, like I said, it's a variable, as it has to be. Either way, you did, you defined it:

def spendingmoney(money):
        return money

Then you need to call it, like this:

spendingmoney(spending_money)

But then, you see, all it does is just return the value it gets. So basically, it's the same!
And then, you added it to the total to be returned..

return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city) + spendingmoney(money)

All these combined, it looks like this:

def trip_cost(city,days,spending_money):
    def spendingmoney(money):
        return money
    return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city) + spendingmoney(spending_money)
trip_cost("Tampa",3,1000)

Notice that it needs to be spelled 'spending_money', not anything else..
Otherwise, it won't work, since the instructions said it.
The last line isn't necessary, but testing functions is an excellent practice!
So how about we print it?

def trip_cost(city,days,spending_money):
   def spendingmoney(money):
       return money
   return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city) + spendingmoney(spending_money)
print trip_cost("Tampa",3,1000)

I hope I've understood and interpreted everything right, as far as you correct me when I'm wrong.
So there you have it..


#5

Try this code it should work it worked for me. Good Luck.

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

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


#6

yeah, you are right. Creating a function for spendingmoney() is not that necessary rather, we can add the spending_money variable. I have worked out by adding the spending_variable directly. Moreover, I did not get the idea of printing trip_cost by placing reserved word print before the function. I have created another variable total_trip_cost which contains the return value of the function called (trip_cost_city) and output the variabale(total_trip_cost). Thanks for the suggestion.I have completed this exercise and taking on to the next one


#7

Thanks for the help. I have completed this exercise and moving on to the next topic


#8

No problem. It was my pleasure.


#12