Codecademy - Python - Tutorial #6

The Cost of Traveling: A Lesson in Functions and Separation of Concerns

When planning a trip, one of the most important factors to consider is the cost. The cost of traveling can vary greatly depending on several factors such as the city, duration of stay, and personal spending habits. In this article, we will explore how to calculate the total cost of traveling using functions in programming.

The first step is to define a function that calculates the cost of a trip. This function should take into account the city, number of days, and other relevant factors. Let's call this function "trip_cost". We can start by defining the function with the following parameters: city, days, and cost per day. The function will return the total cost of the trip.

def trip_cost(city, days, cost_per_day):

# Define the base cost for each city

base_costs = {

'los angeles': 230,

'charlotte': 475,

'tempo': 980,

'pittsburgh': 1000

}

# Check if the city is valid

if city not in base_costs:

return "Invalid city"

# Calculate the total cost

total_cost = base_costs[city] + (days * cost_per_day)

return total_cost

Now that we have defined our function, let's test it out. We can call the function with different cities and days to see how the output changes.

print(trip_cost('los angeles', 3, 200)) # Output: 1130

print(trip_cost('charlotte', 2, 300)) # Output: 950

print(trip_cost('tempo', 1, 1000)) # Output: 980

As we can see, the function returns the total cost of the trip based on the input parameters.

Adding an Extra Argument: Spending Money

However, when planning a trip, there are often additional expenses to consider such as food and souvenirs. To account for this, we need to add another parameter to our function called "spending_money". This parameter will represent the amount of money spent on these extras.

def trip_cost(city, days, cost_per_day, spending_money):

# Define the base cost for each city

base_costs = {

'los angeles': 230,

'charlotte': 475,

'tempo': 980,

'pittsburgh': 1000

}

# Check if the city is valid

if city not in base_costs:

return "Invalid city"

# Calculate the total cost

total_cost = base_costs[city] + (days * cost_per_day)

# Add spending money to the total cost

total_cost += spending_money

return total_cost

Now that we have added this new parameter, let's test it out again.

print(trip_cost('los angeles', 5, 200, 600)) # Output: 1860

print(trip_cost('charlotte', 2, 300, 800)) # Output: 1678

print(trip_cost('tempo', 1, 1000, 400)) # Output: 1384

As we can see, the function now takes into account the additional expenses by adding the "spending_money" parameter to the total cost.

Separation of Concerns

One of the key benefits of using functions is separation of concerns. This means that each function has a single responsibility and does not worry about other parts of the program. In our case, we have separated the calculation of the base cost from the addition of spending money into two separate functions.

This makes it easier to understand and maintain the code, as well as add new features in the future. For example, if we wanted to add a new city or change the way the base cost is calculated, we could simply modify one function without affecting the other parts of the program.

Conclusion

In this article, we have explored how to calculate the total cost of traveling using functions in programming. We have defined two functions: "trip_cost" and an updated version that takes into account "spending_money". We have also discussed the importance of separation of concerns and how it can make our code more maintainable and easier to understand.

By following these principles, we can create powerful programs that solve complex problems and make our lives easier. So, go ahead and plan your next trip or project using functions and separation of concerns!

"WEBVTTKind: captionsLanguage: enoh hey there everybody how is it going you are watching qazi from cleverprogrammer.com and this is codecademy part 5. so let's get started just kidding everybody this is actually part six i just wanted to see if you were paying attention all right so check it out we are at 30 we are getting we're almost getting there okay so first of all i want you to take some time out and pat yourself on the back for getting this far because you are quite far in the project all right but that's the reason i'm sitting here and doing this with you so you feel like we're we're in it together right so let's keep plowing through this and hopefully we will get it done very soon we're at 30 already okay so don't give up now because this is the part where it all starts getting better and you get to use what you've learned all right so we're gonna find python and we're gonna hit continue okay so just for people who are maybe catching you know coming in right now um we are at the part we finish python syntax tip calculator conditionals and control flow pig latin functions and we're starting from taking a vacation hard day at work rough day at school take a load off with a programming vacation that's the one we're on all right so let's click on it and let's get started this one is still has to do with functions so we're gonna continue talking about this one okay so this function is called bigger it takes in first value second value and it figures out which one is bigger so that's really simple i think we can go through it and write it together write a function called answer that takes no arguments and returns a value 42 okay cool so how this function right here works i'm gonna write it out bigger first comma and again variable names here don't matter you can say x and y but in programming generally you want to use better variable names and avoid one letter variable names because you're going to look at it later you're going to be like what the heck was going on here okay so print max of first comma second all right what this does function max figure out what the max is so here if you put in let's say 5 and then 10 the function max is going to go and check hey it's 5 greater than 10 it's going to say no so then it's going to pick 10 as your max and then we just return true so that's really what this thing is doing here we define a function called bigger done we defined a function called bigger it takes you two arguments and then if we print out the larger one and then finally the function itself returns true in step three okay now we try creating a function ourselves so we're gonna write a function or i'm just gonna leave this this here for you guys in comments okay cool define answer and it takes in no arguments so remember the stuff that goes in parentheses those are called arguments or parameters okay if i had it like this this takes in two arguments so this one takes no arguments and returns 42 okay that's all this function does so if i took this function it's a pretty simple function and i went into idle and i pasted it here and then i called this function you see all it does is it just keeps going 42. so i can say something like the answer to life is and we're going to use some string concatenation here is 42. the answer to life is 42. the str thing i did is so it converts this integer data type to a string data type and that way it allows me to actually mix and match them together otherwise it'll give me an error so for example if i don't do the str function on answer i'll get an error that'll say can't convert into object to string object or string implicitly cool save it's mint boom you finish this section planes hotels and automobiles when taking a vacation it's very important to know exactly how much you are going to spend okay so they have a function called wages which takes in an input called hours and then it does 8.35 times hours let's say our hourly hourly rate is equal to 8.35 so we can just you know just just for the sake of clarity here right hourly rate times hours will give you how much is your wage okay so if you write a function wage which is supposed to calculate your how much money you make in a day or however long of a period period it's dependent on information it's dependent on one important information right it's dependent on hours and i would like to argue that this function should also be dependent on your hourly rate right so it should really be like so you give it your hourly rate and your hours and then it spits out something so for example let me show you guys what i mean by this i'm going to take this and i'm going to go here and you guys should follow along and try to use this wage calculator and do something fun right so this is what i always tell my students or really whoever wants to learn programming is don't just look at this stuff do something with this information so maybe build something that helps you so i you know if you have a job do something that'll calculate you know you just put in hours and automatically calculates how much money you make for so for instance i'm going to create this function wages which takes an hourly rate and then hours and then it returns your wages or wage so i'm going to say i'm going to call this function with let's say hourly rate is 20 an hour and you worked uh 15 hours and it returns 300 okay just to keep it simple if i have if i make 10 an hour and i work for 10 hours boom okay and i think we can also do this passing keyword arguments okay so cool all right this is great let's move oh sorry so they want us to create a different function so i'm going to leave this up here define a function called hotel okay hotel cost with one argument nights so already i'm thinking the function is called hotel cost and it's dependent on the nights so i'm thinking if i give this hotel cost the number of nights i lived here then it will give me back the hotel cost okay so that's generally how functions work you name a function based on what it's supposed to tell you and then you put in as input the information it requires to calculate everything okay the hotel costs um 140 per night so the function should total return 140 times nights okay and to make it more readable i can say per night is equal to 140 and then i want to do per night rate times nights okay so when somebody's reading this code they're not going to be like what is 140 where did it come from they now know very clearly that it's the night you know one night rate okay cool start next lesson all right getting there you're going to need to take a plane ride to get to your location okay now before we do that they're giving us an example of a function that does if else if else else if and then else at some point but else if yeah so the function is called fruit color it takes in an argument of fruit and if the fruit was apple it will return red if the fruit was banana returns yellow else if the fruit was pear it returns green so i'm gonna take this function we're going to go to our idle we're going to paste it there and i'm going to say fruit color and i will say so the function is called fruit color but in for an argument it takes in a fruit okay so we're going to give it apple and you're going to see it return red now i'm going to give it banana it's going to turn yellow and if i gave it pear it turns green okay that's how that works okay below your existing code define a function called plain write cost that takes in the string city as input the function should return a different price depending on the locations similar to the code example above below the valid destinations and their corresponding round-trip prices okay we have tampa pittsburgh los angeles and all of that so we're going to write some simple if then statements there are better ways to do it for example using a dictionary but for in our case we're just going to use if then statements so if city equal equal you know charlotte we are going to say return 183 else if city is it doesn't matter if you put double quotes or single quotes just as long as you just remain consistent with it okay also if pittsburgh i just like to put single quotes because then i don't have to hit shift every time i do it oh i did it the other way city equal equal like that return 222 else if city equal equal los angeles returned whoops 475. okay i think this should be good okay that was fairly simple uh you're going to need a rental car in order for you to get there here they have they're showing you how to use comparison operators if score is greater than or equal to 10 then increment the score by 50 or increment the tickets by 50 and if the score is greater than or equal to 7 increment the tickets by 20 and then return tickets so just to give you guys what that means uh if i have something called tickets right and there are 50 tickets and let's say i want to increment the variable tickets by 10. so after this line of code tickets should actually be 60 and not 50. okay so a lot of people will try this and that's what i initially would try but if you go back to tickets you'll see that tickets are still 50 so you haven't done your job correctly how we actually increment the value of tickets so the next time we print out tickets it says 60 is like this tickets is equal to what tickets previously was plus 10 okay now if you do tickets you see it says 60. so this is a pretty common operation okay now if i do tickets again it's 70. so tickets equals tickets plus 10. all right now the shortcut since this is such a common operation we have a shortcut for it in our programming languages python java c plus plus whatever um so the shortcut here is actually this plus equals now if i go back to tickets you'll see it says 80. if i do it again and i go back and take its lc right you'll see it says 100 now so it's just the shortcut is plus equal because it's such a common operation for you guys if you're new write it explicitly right tickets is equal to tickets plus 10. okay and um yeah make sure you write it explicitly all right um and then once you really understand it and you can really use it every single time correctly then move on to the shortcut all right now let's go on to what they're saying below your existing code define a function called define rental car cost with argument days so i'm thinking already that it will tell me the rental car cost based on the number of days that i give it okay calculate the cost of renting car every day you rent the car costs forty dollars okay so every day each day so day rate is equal to 40 is what i would say if you rent a car for seven or more days you get 50 off if days is seven or more okay if if days is equal is greater than or equal to seven then we are going to decrement or no no we're not so i'm going to say total cost is equal to nothing in the start and then total cost minus equals 50 which is the same thing as saying cos minus 50. okay uh total cost we're going to do day rate times days all right that should be our total cost and then we're gonna subtract fifty dollars from the total cost if the rented days was greater than or equal to seven okay and else if you rent the car for three or more days you get twenty dollars off you cannot get both of the above discounts meaning um well meaning you cannot get both of the above discount so if you actually did an if statement it'll go if blah blah will give you a 50 discount and then it'll go on to this if statement and then it'll say oh great since okay let's say you rented the car for 10 days it'll give you the 50 discount and then it'll go to this statement it'll say is days greater than or equal to three it'll say yes and it'll give you a 20 discount as well okay so effectively what you want to be able to do here is so if this part is true the if condition stops elsif is a great use case for that so if this is not true then it's going to go and check if this condition is true okay only if this condition is not true so that's the subtle difference between else if or alif and if and at the end we simply return cost so we return total cost right that's the variable we were using and this should be good boom okay done good pull it together so now you've got your three main costs figured out let's put them in order and use them find the total cost of your trip okay so here they're um so they're just giving you an example to create a function called double which just multiply something by two then they create a function called triple which takes in p as input multiplies p by three so it just multiplies a number by three and then now they're showing you how you can use both of the functions you define in one main function so here in the function called add they're using the function double that's defined up here and they're using the function called triple it's defined up there okay below your existing code define a function called trip cost so we're going to use this function called trip cost it's really going to put everything else in to perspective trip cost that takes two arguments city and days like the example above have your function return the sum of calling the rental car cost days hotel cost and okay so what i'm gonna do here is something along the lines of this return rental car cost and pass in the days we gave it plus hotel costs and the days that we were there plus um plane ride cost and that function takes in the city and luckily we have the city right here it is completely valid to call the hotel cost knights function with the variable days just like the example above okay and this should really do the trick okay what i'm gonna do is we're gonna play around with this a little bit okay i don't want you to do just everything the codecademy tells you to do because you're not going to truly understand it so it's good to take some time out and really play with it the more time you spend like you know playing around with this and not actually just doing the codecademy the better you're gonna get with programming you're gonna it's gonna become more natural to you and you're gonna really understand these concepts okay because understanding these concepts is not oh like check mark i finished this section check mark i know everything about functions i did this i know everything about the math operations it's really when you take out the extra time and just do something for fun for yourself it could be whatever really so i'm going to why is it saying invalid syntax if i paste it in my terminal does the same value syntax yes it does why is it saying invalid syntax um okay let's go to dot it and try it there because i it's really a lot it's a lot more helpful to be able to actually run this and practice it so repple that it does not complain right so we have all these functions now let's try using them okay for example let's try using the hotel cos function let's give it uh we stayed let's say we went to france and we stayed there for five nights and we're gonna say prince hotel cost you can see 700 right five times 140 is 700. okay now let's go to plane right cost and give it city so let's go to print plane ride cost and we give it a city and let's say we give it pits berg okay and now if i run it you can see that it says 222 right there okay um and how it figured it out is that when the city of charlotte it said nope city is tampa nope city is pittsburgh it evaluated to true and then it returned to 222. let's use our third function rental car cost which takes in the days let's give it 20 days and run this so really it's 40 times 40 times 20 that gave us 800 and then it says 20 days is greater than 7 it subtracted 50 and that's how we got 750 right there okay and the last function and let's put this there so then when we use it we can see that actually there's uh it's separated from everything and everything else so this is going to be our trip cost so trip cost and for city we're gonna give it los angeles and for the number of days let's say we stayed there for seven days so we spent one week okay now try to figure out before running this exactly what the cost should be because that will prove to you that you understand this algorithm correctly and i'm just gonna well i guess we can walk through it together as well so seven days so it's gonna come here it's gonna put in uh it's gonna come to this function right trip cost and to calculate the rental car cost is gonna put in seven for where you see days right days right here is going to put in that days into this function rental car cost it's going to go to the function rental car cost put in 7 right here right then it's going to put in 7 right here 40 times 7 what does that give us that gives us 280. it's going to go is 7 7 greater than or equal to 7. it's going to evaluate to true we're going to get our 50 discount it's going to be 280 minus 50 or at 230 so far is going to go to hotel so this is going to be 230 it's going to go to hotel cost function now hotel cost is going to pass in days for this argument nights here okay and since let's say we got 7 is going to put in 7 right here which is going to go up here and put 7 right there and it's going to put 7 right there 140 times 7 gives you 980 okay this whole is thing to evaluate to 980 and then we have the plane right cost city it's dependent on the city so for city we have los angeles okay that's gonna it's gonna be like this pretty much it's gonna go to the function plane ride cost it's gonna put in los angeles it's gonna find it nope this is going to trigger no because los angeles is not equal to charlotte los angeles is not equal to tempo does not equal to pittsburgh is this true yes it is so it returns 475 this whole thing turns to 475 so effectively should be 230 plus 980 plus 475. your total evaluates to 16 85 let's see if we are correct here but that's the general idea okay maybe we could be off but that's the general idea so i'm going to write down my assumption as 1685. it's a good idea to walk through the algorithm by yourself and let's run it trip cost missing arguments oh okay so it should really be seven like this i wrote it incorrectly okay 1685 did we get 16.85 boom so you can see that we nailed that one right we got it perfectly but it's important to understand how all of this works together we wrote three separate functions and then at the end we wrote one main function that uses the three helper functions that we wrote and so basically you never have to worry about these three functions you only call this one so your life is easier and other functions figure out their things but one important thing to note is each function does one thing all right we're not make putting everything together in one place where us it's called separation of concerns we're separating out all of our problem solving to different functions and then we put them together at the end and that ultimately returns something something hopefully accurate all right so that was a lot uh hopefully some of that makes made sense and as you can see we did it correctly so we're gonna hit start next lesson hey you never know you can't expect to only spend money on the plane ride hotel rental car when going on a vacation there also needs to be room for additional costs like fancy food or souvenirs so modify your trip cost function definition add a third argument called spending money modify what the trip cost function does add the variable spending money to the sum that it returns okay plus we could be spending a lot of money so that we also have to take that into account so now the trip cost is really dependent on the city the number of days and spending money okay i mean that's how you would literally define it in real life like hey i'm going on a trip i might ask you what do you think the cost is going to be dependent on and you're probably going to say well it depends what city i go to how many days i stay there and how much money i spend that's really what you're telling the computer as well okay so it's pretty natural to describe it i would think save and submit start next lesson plan your trip nah nice work now that you have it all together let's take a trip what if we want to go to los angeles for five days and brought an extra 600 of spending money so that's awesome because we literally just did this right we opened up our idol not that guy idol and we now hold on i'm sorry we opened up our repple.it and we literally did what they are just about to have you do except they put five days instead of los angeles and they added another argument called spending money all right so now they want us to effectively use our functions right call them so i'm going to say trip cost make sure to do print here it's python 2. so i don't need to put parentheses after the print statement i'm going to say los angeles i'm gonna say five and spending money six hundred dollars and then we save and submit and we get 19.55 cool so that was excellent i we went together and we finished this thing right through now we are on the next section okay let's go back to the course page we just finished taking a vacation we are on unit 5 lists and dictionaries great job seriously guys great job for making it this far because i know it gets tough as you go on and it's conceptually harder there's also more work involved but trust me because this is when it starts to get fun and you can start to do powerful things i mean look down you can already do rock paper scissors a student becomes teacher so being able to manage classes write programs i can intelligently figure out who got a b c d so you don't have to do that and much more like make your own games like battleship okay so we are getting into the stuff that will make you help you do really powerful things so don't get discouraged now let's keep playing through it i will see you guys in the next videooh hey there everybody how is it going you are watching qazi from cleverprogrammer.com and this is codecademy part 5. so let's get started just kidding everybody this is actually part six i just wanted to see if you were paying attention all right so check it out we are at 30 we are getting we're almost getting there okay so first of all i want you to take some time out and pat yourself on the back for getting this far because you are quite far in the project all right but that's the reason i'm sitting here and doing this with you so you feel like we're we're in it together right so let's keep plowing through this and hopefully we will get it done very soon we're at 30 already okay so don't give up now because this is the part where it all starts getting better and you get to use what you've learned all right so we're gonna find python and we're gonna hit continue okay so just for people who are maybe catching you know coming in right now um we are at the part we finish python syntax tip calculator conditionals and control flow pig latin functions and we're starting from taking a vacation hard day at work rough day at school take a load off with a programming vacation that's the one we're on all right so let's click on it and let's get started this one is still has to do with functions so we're gonna continue talking about this one okay so this function is called bigger it takes in first value second value and it figures out which one is bigger so that's really simple i think we can go through it and write it together write a function called answer that takes no arguments and returns a value 42 okay cool so how this function right here works i'm gonna write it out bigger first comma and again variable names here don't matter you can say x and y but in programming generally you want to use better variable names and avoid one letter variable names because you're going to look at it later you're going to be like what the heck was going on here okay so print max of first comma second all right what this does function max figure out what the max is so here if you put in let's say 5 and then 10 the function max is going to go and check hey it's 5 greater than 10 it's going to say no so then it's going to pick 10 as your max and then we just return true so that's really what this thing is doing here we define a function called bigger done we defined a function called bigger it takes you two arguments and then if we print out the larger one and then finally the function itself returns true in step three okay now we try creating a function ourselves so we're gonna write a function or i'm just gonna leave this this here for you guys in comments okay cool define answer and it takes in no arguments so remember the stuff that goes in parentheses those are called arguments or parameters okay if i had it like this this takes in two arguments so this one takes no arguments and returns 42 okay that's all this function does so if i took this function it's a pretty simple function and i went into idle and i pasted it here and then i called this function you see all it does is it just keeps going 42. so i can say something like the answer to life is and we're going to use some string concatenation here is 42. the answer to life is 42. the str thing i did is so it converts this integer data type to a string data type and that way it allows me to actually mix and match them together otherwise it'll give me an error so for example if i don't do the str function on answer i'll get an error that'll say can't convert into object to string object or string implicitly cool save it's mint boom you finish this section planes hotels and automobiles when taking a vacation it's very important to know exactly how much you are going to spend okay so they have a function called wages which takes in an input called hours and then it does 8.35 times hours let's say our hourly hourly rate is equal to 8.35 so we can just you know just just for the sake of clarity here right hourly rate times hours will give you how much is your wage okay so if you write a function wage which is supposed to calculate your how much money you make in a day or however long of a period period it's dependent on information it's dependent on one important information right it's dependent on hours and i would like to argue that this function should also be dependent on your hourly rate right so it should really be like so you give it your hourly rate and your hours and then it spits out something so for example let me show you guys what i mean by this i'm going to take this and i'm going to go here and you guys should follow along and try to use this wage calculator and do something fun right so this is what i always tell my students or really whoever wants to learn programming is don't just look at this stuff do something with this information so maybe build something that helps you so i you know if you have a job do something that'll calculate you know you just put in hours and automatically calculates how much money you make for so for instance i'm going to create this function wages which takes an hourly rate and then hours and then it returns your wages or wage so i'm going to say i'm going to call this function with let's say hourly rate is 20 an hour and you worked uh 15 hours and it returns 300 okay just to keep it simple if i have if i make 10 an hour and i work for 10 hours boom okay and i think we can also do this passing keyword arguments okay so cool all right this is great let's move oh sorry so they want us to create a different function so i'm going to leave this up here define a function called hotel okay hotel cost with one argument nights so already i'm thinking the function is called hotel cost and it's dependent on the nights so i'm thinking if i give this hotel cost the number of nights i lived here then it will give me back the hotel cost okay so that's generally how functions work you name a function based on what it's supposed to tell you and then you put in as input the information it requires to calculate everything okay the hotel costs um 140 per night so the function should total return 140 times nights okay and to make it more readable i can say per night is equal to 140 and then i want to do per night rate times nights okay so when somebody's reading this code they're not going to be like what is 140 where did it come from they now know very clearly that it's the night you know one night rate okay cool start next lesson all right getting there you're going to need to take a plane ride to get to your location okay now before we do that they're giving us an example of a function that does if else if else else if and then else at some point but else if yeah so the function is called fruit color it takes in an argument of fruit and if the fruit was apple it will return red if the fruit was banana returns yellow else if the fruit was pear it returns green so i'm gonna take this function we're going to go to our idle we're going to paste it there and i'm going to say fruit color and i will say so the function is called fruit color but in for an argument it takes in a fruit okay so we're going to give it apple and you're going to see it return red now i'm going to give it banana it's going to turn yellow and if i gave it pear it turns green okay that's how that works okay below your existing code define a function called plain write cost that takes in the string city as input the function should return a different price depending on the locations similar to the code example above below the valid destinations and their corresponding round-trip prices okay we have tampa pittsburgh los angeles and all of that so we're going to write some simple if then statements there are better ways to do it for example using a dictionary but for in our case we're just going to use if then statements so if city equal equal you know charlotte we are going to say return 183 else if city is it doesn't matter if you put double quotes or single quotes just as long as you just remain consistent with it okay also if pittsburgh i just like to put single quotes because then i don't have to hit shift every time i do it oh i did it the other way city equal equal like that return 222 else if city equal equal los angeles returned whoops 475. okay i think this should be good okay that was fairly simple uh you're going to need a rental car in order for you to get there here they have they're showing you how to use comparison operators if score is greater than or equal to 10 then increment the score by 50 or increment the tickets by 50 and if the score is greater than or equal to 7 increment the tickets by 20 and then return tickets so just to give you guys what that means uh if i have something called tickets right and there are 50 tickets and let's say i want to increment the variable tickets by 10. so after this line of code tickets should actually be 60 and not 50. okay so a lot of people will try this and that's what i initially would try but if you go back to tickets you'll see that tickets are still 50 so you haven't done your job correctly how we actually increment the value of tickets so the next time we print out tickets it says 60 is like this tickets is equal to what tickets previously was plus 10 okay now if you do tickets you see it says 60. so this is a pretty common operation okay now if i do tickets again it's 70. so tickets equals tickets plus 10. all right now the shortcut since this is such a common operation we have a shortcut for it in our programming languages python java c plus plus whatever um so the shortcut here is actually this plus equals now if i go back to tickets you'll see it says 80. if i do it again and i go back and take its lc right you'll see it says 100 now so it's just the shortcut is plus equal because it's such a common operation for you guys if you're new write it explicitly right tickets is equal to tickets plus 10. okay and um yeah make sure you write it explicitly all right um and then once you really understand it and you can really use it every single time correctly then move on to the shortcut all right now let's go on to what they're saying below your existing code define a function called define rental car cost with argument days so i'm thinking already that it will tell me the rental car cost based on the number of days that i give it okay calculate the cost of renting car every day you rent the car costs forty dollars okay so every day each day so day rate is equal to 40 is what i would say if you rent a car for seven or more days you get 50 off if days is seven or more okay if if days is equal is greater than or equal to seven then we are going to decrement or no no we're not so i'm going to say total cost is equal to nothing in the start and then total cost minus equals 50 which is the same thing as saying cos minus 50. okay uh total cost we're going to do day rate times days all right that should be our total cost and then we're gonna subtract fifty dollars from the total cost if the rented days was greater than or equal to seven okay and else if you rent the car for three or more days you get twenty dollars off you cannot get both of the above discounts meaning um well meaning you cannot get both of the above discount so if you actually did an if statement it'll go if blah blah will give you a 50 discount and then it'll go on to this if statement and then it'll say oh great since okay let's say you rented the car for 10 days it'll give you the 50 discount and then it'll go to this statement it'll say is days greater than or equal to three it'll say yes and it'll give you a 20 discount as well okay so effectively what you want to be able to do here is so if this part is true the if condition stops elsif is a great use case for that so if this is not true then it's going to go and check if this condition is true okay only if this condition is not true so that's the subtle difference between else if or alif and if and at the end we simply return cost so we return total cost right that's the variable we were using and this should be good boom okay done good pull it together so now you've got your three main costs figured out let's put them in order and use them find the total cost of your trip okay so here they're um so they're just giving you an example to create a function called double which just multiply something by two then they create a function called triple which takes in p as input multiplies p by three so it just multiplies a number by three and then now they're showing you how you can use both of the functions you define in one main function so here in the function called add they're using the function double that's defined up here and they're using the function called triple it's defined up there okay below your existing code define a function called trip cost so we're going to use this function called trip cost it's really going to put everything else in to perspective trip cost that takes two arguments city and days like the example above have your function return the sum of calling the rental car cost days hotel cost and okay so what i'm gonna do here is something along the lines of this return rental car cost and pass in the days we gave it plus hotel costs and the days that we were there plus um plane ride cost and that function takes in the city and luckily we have the city right here it is completely valid to call the hotel cost knights function with the variable days just like the example above okay and this should really do the trick okay what i'm gonna do is we're gonna play around with this a little bit okay i don't want you to do just everything the codecademy tells you to do because you're not going to truly understand it so it's good to take some time out and really play with it the more time you spend like you know playing around with this and not actually just doing the codecademy the better you're gonna get with programming you're gonna it's gonna become more natural to you and you're gonna really understand these concepts okay because understanding these concepts is not oh like check mark i finished this section check mark i know everything about functions i did this i know everything about the math operations it's really when you take out the extra time and just do something for fun for yourself it could be whatever really so i'm going to why is it saying invalid syntax if i paste it in my terminal does the same value syntax yes it does why is it saying invalid syntax um okay let's go to dot it and try it there because i it's really a lot it's a lot more helpful to be able to actually run this and practice it so repple that it does not complain right so we have all these functions now let's try using them okay for example let's try using the hotel cos function let's give it uh we stayed let's say we went to france and we stayed there for five nights and we're gonna say prince hotel cost you can see 700 right five times 140 is 700. okay now let's go to plane right cost and give it city so let's go to print plane ride cost and we give it a city and let's say we give it pits berg okay and now if i run it you can see that it says 222 right there okay um and how it figured it out is that when the city of charlotte it said nope city is tampa nope city is pittsburgh it evaluated to true and then it returned to 222. let's use our third function rental car cost which takes in the days let's give it 20 days and run this so really it's 40 times 40 times 20 that gave us 800 and then it says 20 days is greater than 7 it subtracted 50 and that's how we got 750 right there okay and the last function and let's put this there so then when we use it we can see that actually there's uh it's separated from everything and everything else so this is going to be our trip cost so trip cost and for city we're gonna give it los angeles and for the number of days let's say we stayed there for seven days so we spent one week okay now try to figure out before running this exactly what the cost should be because that will prove to you that you understand this algorithm correctly and i'm just gonna well i guess we can walk through it together as well so seven days so it's gonna come here it's gonna put in uh it's gonna come to this function right trip cost and to calculate the rental car cost is gonna put in seven for where you see days right days right here is going to put in that days into this function rental car cost it's going to go to the function rental car cost put in 7 right here right then it's going to put in 7 right here 40 times 7 what does that give us that gives us 280. it's going to go is 7 7 greater than or equal to 7. it's going to evaluate to true we're going to get our 50 discount it's going to be 280 minus 50 or at 230 so far is going to go to hotel so this is going to be 230 it's going to go to hotel cost function now hotel cost is going to pass in days for this argument nights here okay and since let's say we got 7 is going to put in 7 right here which is going to go up here and put 7 right there and it's going to put 7 right there 140 times 7 gives you 980 okay this whole is thing to evaluate to 980 and then we have the plane right cost city it's dependent on the city so for city we have los angeles okay that's gonna it's gonna be like this pretty much it's gonna go to the function plane ride cost it's gonna put in los angeles it's gonna find it nope this is going to trigger no because los angeles is not equal to charlotte los angeles is not equal to tempo does not equal to pittsburgh is this true yes it is so it returns 475 this whole thing turns to 475 so effectively should be 230 plus 980 plus 475. your total evaluates to 16 85 let's see if we are correct here but that's the general idea okay maybe we could be off but that's the general idea so i'm going to write down my assumption as 1685. it's a good idea to walk through the algorithm by yourself and let's run it trip cost missing arguments oh okay so it should really be seven like this i wrote it incorrectly okay 1685 did we get 16.85 boom so you can see that we nailed that one right we got it perfectly but it's important to understand how all of this works together we wrote three separate functions and then at the end we wrote one main function that uses the three helper functions that we wrote and so basically you never have to worry about these three functions you only call this one so your life is easier and other functions figure out their things but one important thing to note is each function does one thing all right we're not make putting everything together in one place where us it's called separation of concerns we're separating out all of our problem solving to different functions and then we put them together at the end and that ultimately returns something something hopefully accurate all right so that was a lot uh hopefully some of that makes made sense and as you can see we did it correctly so we're gonna hit start next lesson hey you never know you can't expect to only spend money on the plane ride hotel rental car when going on a vacation there also needs to be room for additional costs like fancy food or souvenirs so modify your trip cost function definition add a third argument called spending money modify what the trip cost function does add the variable spending money to the sum that it returns okay plus we could be spending a lot of money so that we also have to take that into account so now the trip cost is really dependent on the city the number of days and spending money okay i mean that's how you would literally define it in real life like hey i'm going on a trip i might ask you what do you think the cost is going to be dependent on and you're probably going to say well it depends what city i go to how many days i stay there and how much money i spend that's really what you're telling the computer as well okay so it's pretty natural to describe it i would think save and submit start next lesson plan your trip nah nice work now that you have it all together let's take a trip what if we want to go to los angeles for five days and brought an extra 600 of spending money so that's awesome because we literally just did this right we opened up our idol not that guy idol and we now hold on i'm sorry we opened up our repple.it and we literally did what they are just about to have you do except they put five days instead of los angeles and they added another argument called spending money all right so now they want us to effectively use our functions right call them so i'm going to say trip cost make sure to do print here it's python 2. so i don't need to put parentheses after the print statement i'm going to say los angeles i'm gonna say five and spending money six hundred dollars and then we save and submit and we get 19.55 cool so that was excellent i we went together and we finished this thing right through now we are on the next section okay let's go back to the course page we just finished taking a vacation we are on unit 5 lists and dictionaries great job seriously guys great job for making it this far because i know it gets tough as you go on and it's conceptually harder there's also more work involved but trust me because this is when it starts to get fun and you can start to do powerful things i mean look down you can already do rock paper scissors a student becomes teacher so being able to manage classes write programs i can intelligently figure out who got a b c d so you don't have to do that and much more like make your own games like battleship okay so we are getting into the stuff that will make you help you do really powerful things so don't get discouraged now let's keep playing through it i will see you guys in the next video\n"