So that when you put the amount of days as a parameter, it will automatically plug it into both the rental_car_cost and hotel_cost functions. So there are two parameters and three functions because one of the parameters (days) works for two of the functions.
Majority of code removed. Checker appears to assume no error checking and rejects code as a result. Reference the delta between hotel_cost and hotel_cost_bad functions and with 0 days the trip_cost would return 80 vs 220. Hotel pays you for not staying there.
print “Start”
def hotel_cost(nights):
if (type(nights) == int) or (type(nights) == float):
if int(nights) >=0 :
return 140*int(nights)
else: #int(nights) <= 0:
return 0
else:
print “arg needs to be int or float”
return False
def hotel_cost_bad(nights):
if (type(nights) == int) or (type(nights) == float):
if int(nights) >=0 :
return 140int(nights)
else: #int(nights) <= 0:
print “Allow hotel to pay you for not staying there!”
return 140int(nights) #0
else:
print “arg needs to be int or float”
return False
print “test hotel”
print hotel_cost(1)
print hotel_cost(0)
def plane_ride_cost(city):
print (type(city) == str)
print city.lower()
…
elif city.lower() == “tampa”:
return 220
…
#print “debug”
#plane_ride_cost(“Charlotte”)
def rental_car_cost (days):
…
print “cost_analysis definition function same function as trip_cost, but cost analysis function has some error checking”
def cost_analysis (city, days):
trip_sum = rental_car_cost(days)
trip_sum += hotel_cost(days -1)
trip_sum += plane_ride_cost(city)
print “trip_sum %s to %s for %s days” %(trip_sum, city, days)
return trip_sum
def trip_cost (city, days):
trip_sum = rental_car_cost(days)
trip_sum += hotel_cost_bad(days -1)
trip_sum += plane_ride_cost(city)
print “trip_sum %s to %s for %s days” %(trip_sum, city, days)
return trip_sum
print “Sample error checking start…”
print “…Example 1…”
print “cost_analysis function call”
sum1 = cost_analysis(“los angeles”, 5.6)
print “trip_cost function call”
sum2 = trip_cost(“los angeles”, 5.6)
evaluation = sum1 == sum2
print “Result funtions return same value %s” %(evaluation)
print “…Example 2…”
sum1 = cost_analysis(“tampa”, 0)
sum2 = trip_cost(“tampa”, 0)
evaluation = sum1 == sum2
print “Result funtions return same value %s” %(evaluation)
print “Sample error checking end…”
Hi - I got 2 questions:
-
Why do we need to specify parameters for the function of adding the total of all three costs if these parameters are already included in each cost category - isn’t this complicating matters beyond what is needed? A simple addition of three totals shouldn’t need further parameters, no?
-
How does Python know we are referring to days-1 if the initial function was defined with “nights” as the parameter? Nowhere was this change put into the system?
Thanks for your help
literally same. i checked my code against the example & answer and i see zero differences
I’ve had the same issues on a number of exercises. But if I hit “View Solution”, erase their code, then paste mine, it accepts my code.
Same here. But when I put my code, it said define trip_cost when I had already done so. THE CODE IN THE SOLUTION WAS LITERALLY THE SAME!
My code keeps running and running with no end on sight.
I can’t find any errors.
def hotel_cost (days-1):
return 140 * (days-1)
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):
cost -= 50
elif (days >=3):
cost -= 20
return cost
def trip_cost(city , days):
return hotel_cost(days-1) + plane_ride_cost(city) + rental_car_cost(days)
print trip_cost(“Tampa”, 2)
I am facing a problem in my code everytime i give input and it doesnt gives any output any one help me to understand where is the problem in my code.
My Codes are -
Hotel Cost Per nights :
def hotel_cost(days):
return 140 * days
Flight cost for different cities:
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
Per Day Car Ride cosy:
def rental_car_cost(days):
per_day_cost = 40 * days
if days >= 7:
per_day_cost = per_day_cost - 50
return per_day_cost
elif days >=3:
per_day_cost = per_day_cost - 20
return per_day_cost
else:
return per_day_cost
#Total trip Cost:
def trip_cost(city,days):
total = rental_car_cost(days)+hotel_cost(days - 1)+ plane_ride_cost(city)
Have a look at this FAQ concerning code formatting which helps immensely when posting queries-
What is the exact issue? Does it throw an error or does the code execute? What output were you expecting from your given input? Is there code to perform this?
This message pop “trip_cost(‘Tampa’, 4) returned None instead of the correct value 780”. I don’t understand where i am getting wrong in functions my codes run but i did’nt get any output.
Here’s my code:
“”"
Hotel Cost Per nights :
def hotel_cost(days):
return 140 * days
Flight cost for different cities:
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
Per Day Car Ride cosy:
def rental_car_cost(days):
per_day_cost = 40 * days
if days >= 7:
per_day_cost = per_day_cost - 50
return per_day_cost
elif days >=3:
per_day_cost = per_day_cost - 20
return per_day_cost
else:
return per_day_cost
#Total trip Cost:
def trip_cost(city,days):
total = rental_car_cost(days)+hotel_cost(days - 1)+ plane_ride_cost(city)
“”"
It’s a backtick/backquote character you need to wrap chnks of code with-
` ` `
code goes here
` ` `
If it’s a message that you didn’t code then I’d assume it’s on of the background tests that will be run on your code in certain lessons to make sure it meets the requirements.
Breaking down that message the function trip_cost
was called with two arguments ('Tampa', 4)
and the value returned was None
rather than the expected value 780. Since you don’t actually use a None object anywhere it must have come from somewhere else.
The most likely candidate is that if you have a function that doesn’t actually return anything it defaults to returning None
. Could this have occurred in your code?
So I tried the same and got a syntax error for def hotel_cost(days - 1):
I think we are supposed to leave the argument as is def hotel_cost(nights)
but then when return the sum we call it as def hotel_cost(days - 1) .
In the example they showed, that you names of the parameters can be different.
Cheers
Andy
The syntax error is because you can’t use expressions for parameter names. It’s basically the same as using x - 3 = 'fifteen'
, it’s trying to assign to a name x - 3
which is not a valid parameter name. You can pass expressions as arguments to a function though (e.g. print(10 - 3)
), they simply get evaluated before they are passed. It’s the bit about creating a new name or reference with invalid syntax that throws an error.
For some reason this isnt working
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 ("Nothing Defined")
def rental_car_cost(days):
cost = days * 40
if days >= 3 and days < 7:
cost -= 20
elif days >= 7:
cost -= 50
return cost
def trip_cost(city, days):
return rental_car_cost(days) + plane_ride_cost(city) + hotel_cost(days - 1)
its saying make sure to define a function named trip_cost
I checked the solution code against mine which looked exactly like yours.
The correct code uses ifs, instead of the elifs in the city section.
excuse me. I cannot scroll all the way down to solve this problem
This is kind of a funny code, if you take a vacation of zero days, the hotel pays you!
I’m stuck and don’t know what is wrong can anyone help?
The error I’m getting is:
“trip_cost should take exactly three parameters: city, days, and spending_money (in that order).”
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:
cost = cost - 50
return cost
elif (days >= 3) and (days < 7):
cost = cost - 20
return cost
def trip_cost(city, days, spending_money):
return plane_ride_cost(city) + hotel_cost(days - 1) + rental_car_cost(days) + spending_money
print trip_cost(‘Los Angeles’, 5, 600)
it has some glitch just go to view solution and click replace with with my code then you’ll get to learn further