Excellent work! 👏 In this lesson, you’ve covered a major fundamental programming concept used in the majority of all programs to organize code into reusable blocks. To recap, we explored:
- What problems arise in our programs without functions
- What functions are and how to write them
- How whitespace plays an important role in how a program will execute our code and more importantly distinguish function code from non-function code
- How to pass input values into our functions using parameters and arguments
- The difference between built-in and user-defined functions and some common built-in functions python offers
- How we can reuse function output with the
returnkeyword, as well as multiple returns.
- Where variables can be accessed in our programs that use functions
Let’s practice putting all these concepts together!
Alright, this is it. We are going to use all of our knowledge of functions to build out TripPlanner V1.0.
First, like in our previous exercises, we want to make sure to welcome our users to the application.
Create a function called
trip_planner_welcome() that takes one parameter called
name. The function should use
print() to output a message like this:
Welcome to tripplanner v1.0 <Name Here>
<Name Here> represents the parameter variable of
name we defined.
Next, we are going to generate messages for a user’s planned trip.
Create a function called
destination_setup() that will have four parameters in this exact order:
Give the parameter
mode_of_transport a default value of
"Car". The program will error out if we run it since we have not defined a function body yet. Don’t worry we will do that in the next step.
Next, we are going to write four
print() statements in our function. The output on this function call should look like this:
Your trip starts off in <origin> And you are traveling to <destination> You will be traveling by <mode_of_transport> It will take approximately <estimated_time> hours
Each of these
print() statements use a different parameter from our function
estimated_time parameter will come in the form of a decimal. Make sure to use
str() to convert the parameter in your print statement.
Lastly, we are going to define a function called
estimated_time_rounded() that will allow us to calculate a rounded time value based on a decimal for our user’s trip.
An example call for this function will look like this:
2 represents 2 hours and
.43 represents 43 minutes.
Define the function
estimated_time_rounded() with a single parameter
estimated_time. The function should do two things:
- Create a variable called
rounded_timethat is the result of calling the
round()built-in function on the parameter
Great job! 👏
We have successfully finished our first version of the trip builder application. Go ahead and uncomment the provided function calls and fill in the values with whatever you like.
Once have filled in the arguments, run the program to see it all in action.