Doing an assignment in Python (Math 30800, Section R, Fall 2015)

This page demonstrates how you would do a programming assignment. Please try to follow along to practice. Lets say we were to get the following two problems.

Inside the file pa0.py place two functions which solve the following problems:
  1. Write a function product(x,y,z) which takes as input three numbers x, y and z and returns their product.
  2. The Fibonacci sequence is determined inductively by F0=0, F1=1 and Fn+1=Fn+Fn-1 whenever n ≥ 1 is an integer. Write a function fibonacci(n) which takes as input an integer n ≥ 0 and returns the n-th Fibonacci number.

Before we start, let me make a remark. You should not expect to get everything right the first time. I consistently get errors when I program. At first it will be difficult to figure out what the errors mean, but it will get easier as you program more. Don't be afraid to experiment!

To start the assignment, we open up IDLE. You should see a window similar to the following:

Screenshot of initial IDLE window.

This is the interpreter window.

Click the File menu and select New File. A new window should open which looks similar to this:

Screenshot of initial program window.

I recommend saving the file first to set the file name. Click the File menu and select Save as. Move to the folder where you want to save it, then type pa0.py as the file name and click the Save button.

This window is called the program window. Type your solution to the first programming problem. It should look something like this:

Programming window after entering the first program.

Save your work by selecting Save from the File menu. Then click on the Run menu and select Run module.

If all when well you'll see a line looking like
>>> ================================ RESTART ================================
appear in the interpreter window. The program was loaded. Now in the interpreter window type product(2,3,4) next to the last >>> prompt. It should print out the answer and look like the following:

Interpreter after the command is given.

You might try inputting a couple more values to make sure it works.

Now lets move on to the second assignment. Move back to the program window. We should spend some time thinking about how to solve the problem. The first line of the function will be:
def fibonacci(n):
Type this at the end of the file (after the product function).

We need to repeatedly add numbers many times to find Fn. The only thing we've learned (so far) which can do this is a while loop. Now lets think about how we can compute successive Fibonacci numbers. The first two values of the Fibonacci sequence are 0 and 1. Lets store them in two variables a and b. The next Fibonacci number F2 is then a+b. Lets store this in the variable c. Lets also define k=2. We'll use the value of k to "remember" that the variable c stores Fk. The value stored in a will always be Fk-2 and the value stored in a will always be Fk-1 (at the end of any run the statements in the while loop). Every time we run through the statements in the while loop, we will be updating the values of all these variables, increasing k by one. We will want to run the loop until k equals n. Thinking this way, we arrive at a program like this:

First version of fibonacci function.

Now save the file, run it, and test it in the interpreter window. To test it, we might have it compute the 5th Fibonacci number, F5=5. It works, but this wasn't a very good test. A better test would be to have it print the first few of the Fibonacci numbers. We can use a while loop for this (as shown below). When entering a while loop in the interpreter remember to end the line with a colon. The following lines will be indented. To finish entering the while loop, you need to hit enter twice. You should get something like the following:

Testing the fibonacci function.

Do you notice the error above? The first Fibonacci number printed should be F0=0, but it printed 1. Entering fibonacci(0) verifies the problem. We can fix this by adding a simple if statement to the function:

Fixed fibonacci function.

Save the file, run it, and test it again. Now we get the right sequence:

Retesting the fibonacci function.

The saved file pa0.py is what you turn in.

For your convenience, you can download the pa0.py file I created and the text of the interpreter window that I entered.


Return to the Programming Resources
Return to the course website
Last modified on August 17, 2018.
[check html] [check css]