From the course: Programming Foundations: Fundamentals

Solution: Favorite cities - Python Tutorial

From the course: Programming Foundations: Fundamentals

Solution: Favorite cities

(upbeat music) - Welcome back. I hope you had fun thinking of your favorite cities and writing the code for this challenge. Here's the solution I came up with. On line number one, I just have a comment. It prints out the name of a favorite city, because that's what our function does. Then on line number two, we have the function definition. Notice that we're using the def keyword. This is how we let Python know that we are creating a function. It takes in one parameter named Name. Then, inside of our function on line number three, we use the variation of the print method that first displays a string, one of my favorite cities is, and then it prints out the value of the name variable with a space before it. On line number five, I call the favorite city function with one of my favorite cities, Santa Barbara, California. Then on line number six, I call it again with another one of my favorite cities, Asheville, North Carolina. And finally, on line number seven, we call our function using Amsterdam, The Netherlands. Let's run this code and check out the output. Yay, it called the function three different times, with each of my favorite cities. Cool! Were you able to use a function to print out your favorite cities? How'd it go? Did you come up with something similar? I hope so. Breaking up our code into reusable chunks helps us to develop programs that are easier to read, write, and debug. Now you have one more tool in your programmer's belt.

Contents