Unit 2: User Defined Functions, Lists and For-Loop Basics.

Lesson 11: Basic syntax of a function definition.

We would like to be able to define our own functions so that we are not dependent on the few, built-in functions that python comes with. The following lessons explore this topic:



The effect of changing the order of arguments in a function call:


A sneak peak at importing and the returning values from a user-defined function.


Exercise Set 2.1:

Read this! Be sure to do all of the problem sets. Follow all of the directions carefully and let me know if you are uncertain about what the instructions are telling you to do.

If you have not done so already, create a new folder somewhere on your computer and give it a name like: Hillisde_Computer_Science. Inside of this folder, make another folder called CS_Exercise_Set_1. If you are doing this on a school computer, change those names so that they include your name at the beginning. Now be sure to save all of your work from this section inside of that CS_Exercise_Set_1 folder.

In this exercise set, you will apply what you have learned about the syntax of user-defined functions. You will also get a taste of organizing a program around some of the tools that your have created to solve pieces of a problem.

 1.) Write a function that figures out how old someone is (roughly…we will ignore day and month for now.) based on the current year and the year they were born. The current year and year of birth should be parameters of the function; Prompt the user for these values and then pass them to the function in the function call as arguments. When the function appears to be working, do a “sanity check”. Does the function accurately determine your age?

Tips: Start simple. At first, just try to get your function working without prompting for user-input. Just temporarily define some variables that you will use as arguments with assignment statements like this:

current_year = 2013

birth_year = 1982

…after you get the code working this way, you will know that your function works. Then modify it so that it can handle user input by adding raw_input statements.


2.) Write a function that takes a first name, middle name and last name as parameters

then concatenates and returns a string containing a nicely formatted (spaces in the correct places) name.

3.) Combine your code from 1.) and 2.) into one program that prompts a person for their first, middle and last name separately. Then it should prompt them for the current year and their year of birth. Finally it should use your two functions to help you get a string containing their name and then an integer representing their age. Finally have the program print a message that greets them using their full name and tells them how old they are.

Hint: You should not have to modify your functions from 1.) or 2.). Make sure that you do not write any code that does the work that your functions can do.

4.)  Challenge: Write a script that prompts the user for a distance in feet and then converts it to miles,meters and kilometers. Berate the user for working in feet.

Hints: Remember what happens when you divide integers by integers? The float(someInteger) function can be used to convert integers to floats, where the variable someInteger is just whatever integer you need to convert. )

5.) Our course roughly follows this great online text: Think Python. You can view it as a PDF or through your browser.  The author, Allen B. Downey, has provided some great programming exercises. For this exercise, do “Exercise 3” found here. This one is pretty tough, so be patient with yourself and python as you work it out. As always, do not hesitate to ask me for help.

Also, it might be helpful to read the section about scope here….I do not think he uses the word “scope” in that section, but it is about how variables and parameters are “local”. We will go into more detail on that in future videos.

Extra Stuff: If you have some extra time and are feeling ambitious, try the rest of the Exercises from think python section 3.16. You can find them by clicking the link from problem 5.) and just scrolling down the page a little.

If you do not have the time or ambition, compromise by thinking about his exercises and then studying the solutions that he provides via links at the bottom of each Exercise description. I will say that again: Solutions are provided. Just click the links and paste the text into a new window in IDLE to play around with them and figure out how he solves these problems. Even if you do solve these yourself, you should probably check out his solutions because they are pretty informative. Reading other people’s code is a great way to learn!

Lesson 12: Scope, local and global variables.


Lesson 13: Turtles, Lists and Loops


Note: In the following video, I try to pause the recording software that I am using but my glitchy machine does not register the mouse-click. The result is something I missed in editing: 2 minutes of extremely uninformative video. I recommend that when the video gets to time 6:27 , you should skip ahead to about 8:38. Trust me, you won’t miss anything important!


Attention: I mixed up my quiz content a little so there is just one huge quiz for the previous video and the video that follows. Before you  watch the video below, you should probably do the first half of the quiz (Quiz 13B below). You will know when to stop and watch the next video because there is a section header in the quiz letting you know which questions correspond to the video below.


Exercise set 2:

1.) Write code that defines three lists consisting of food names (strings), a second list with their price per pound and a third list with how many pounds of each item that a customer wants to buy.

Now write a for loop that prints out put for each item such as the following:

You want 2.5 lbs of cabbage at 2.00 dollars per pound. That is 5.00 worth of cabbage!

You want 1.0 lbs of broccoli at 2.50 dollars per pound. That is 2.50 dollars worth of broccoli!”

You want 3.0 lbs of ham at 0.5 dollars per pound. That is 1.50 dollars worth of ham!”





2.) Modify your code from problem 1 so that it prints the total amount spent after all of the above print statements.

3.) define a list with the following structure :

food_price_quantity = [[first_food,price_1,quantity_1], [second_food,price_2,quantity_2],… ]

Write a program with the same output as above that uses this list structure.


4.) The Fibonacci numbers are a list of numbers that are generated by adding the previos two numbers to get the new number. The first several numbers are:


Write a program that computes the first 100 Fibonacci numbers.

Hints: You can either store all of the numbers in a list as you go or you can just store the last two numbers. You can add an item to the end of a list using the append function: The command, my_list.append(3), will append the value 3 to the end of your list. You might also find the len function useful. len(my_List) returns the length of my_List.