lab03 : Lab03 - Accumulator Functions and User Input

num ready? description assigned due
lab03 true Lab03 - Accumulator Functions and User Input Wed 04/26 08:00AM Fri 04/28 11:59PM

Lab03: Factorial Function & Coin Counter


Goals for this lab

By the time you have completed this lab, you should be able to:

  1. Write functions that use what the textbook calls the "Accumulator Pattern"
  2. Write functions that get user inputs from standard input devices (i.e. keyboard).

Step by Step Instructions

Step 0: Get together with your assigned lab partner.

Step 1: Log in, and create a lab03 directory Decide which one of you will be the first pilot for this lab, and then log in to the pilot’s account and create a directory called lab03 inside his/her cs8 folder. You will work in this account for the rest of the lab, but remember to switch often between pilot and navigator roles during the course of the lab.

Step 2: Bring up IDLE as usual, and open a new window for function definitions Start IDLE. Then, select "File->New Window" and add a comment at the top of this new file like the following (with the proper substitutions of course):

# lab03.py
# Your name(s), today's date
# a factorial function and a coin counter function

Then, save it under the name lab03.py inside your lab03 directory.

Step 3: Practice writing a function to accumulate products

(Exercise 2.12 from page 56 of the text). Together we will write a function called factorial that computes the product of the first N numbers, where N is a parameter. For example, factorial(4) is calculated as 1 * 2 * 3 * 4, so the function should return the value 24. We will assume N is greater than 0.

Be sure to understand the purpose and meaning of each of the following instructions before you execute them.

  1. Skip a blank line (in your lab03.py file), and then type a brief comment for a factorial function such as the following:
      # factorial - returns n factorial 
      # assumes n is greater than or equal to 0
      
  2. On the very next line, type the function header as follows:
    def factorial(n):
  3. We need an "accumulator" variable for the result. Since the accumulation will be multiplicative (instead of additive), this variable should be initialized to 1 (and the statement must be indented as it is inside the function):
      result = 1
      
  4. Now it is time to loop through the values that must be multiplied, and we will use the Python range function for the purpose. First let's think about the sequence we need to multiply: 1*2*...*n. We can skip the 1 and still get the same result, but we can't skip the n. So that means we can start the range at 2, but the stop value must be n+1 to include n as the last value. Inside the loop we must do the multiplications and accumulate the product in our result variable. Here is the whole thing (since we already initialized result to 1 above; and notice the body of the loop is indented one more time):
        
      for i in range(2, n+1):
            result = result * i
    
  5. Just one last thing to do is return the result, and then type a blank line to indicate the function is complete:
        
      return result
      

Save and run the module. Then test the function a few times (in the Python shell window) by comparing its results to ones that you calculate manually. Here is one example run of our solution:

>>> factorial(5)
120

Step 4: Couting Coins

Write a program that allows the user to enter a number of quarters, dimes, and nickels and then outputs the monetary value of the coins in cents. For example, if the user enters 2 for the number of quarters, 3 for the number of dimes, and 1 for the number of nickels, then the program should output that the coins are worth 85 cents.

The program should ask the user by printing out a question (output) and the wait for the user to answer it (input). User input is followed by pressing the RETURN key.

We have learned how to handle outputs to standard devices, i.e. using the print( ) function. But how do we handle inputs? In Python, use the built-in function input( ), like this example, which you can try out in the Python interpreter:

>>> name = input("What is your name? ")
What is your name? Jimbo
>>> name
'Jimbo'

The default for input( ) is that the variable is a string, like name = ‘Jimbo’ in the previous example. So you can use the variable as you would any string, for example (continuing from the prior example):

>>> len(name)
5
>>> name[2]
'm'

If the user had entered 33 at the input prompt, then the variable name would now hold ‘33’ as a string (not as a number!) If you want to ensure that the input is accepted as something else, like an integer, then do as this example shows (note how we would modify the input( ) function):

>>> number = int( input("What is your age? ") )
What is your age? 21
>>> number
21
>>> number + 10
31

Just as you did with the previous function, skip a blank line (in your lab03.py file), and then type a brief comment, followed by the function header (what will you call this function?)

This function should print a string of text to the terminal before getting each piece of input from the user. A session should look like the following example with possibly different numbers for inputs and the output (so the following is just ONE example of how the function might run):

Enter number of quarters: 4
Enter number of dimes: 12
Enter number of nickels: 3
The total is: 235 cents.

Step 5: Show off your work and get credit for the lab

Get your TA's attention to inspect your work, and to record your lab completion.

Step 5a. Your final submission, lab03.py, should now have 2 functions defined therein: one for the factorial function and the other for the coin counter function.

In the unlikely event that you must complete this assignment at CSIL, then submit it with the turnin program. You MUST have both your name and your partner’s name in the file in order to receive credit. Remember that the original pilot needs to do this step, since that is whose account you have been using in Phelps 3525. Use the following command after you cd into your cs8/lab03 directory:

turnin Lab03@cs8 lab03.py

After answering the questions, be sure to wait for the message indicating success.


Evaluation and Grading

Each student must accomplish the following to earn full credit for this lab:

  • Turn in a complete and accurate Hw3.
  • lab03.py is saved in original pilot's ~/cs8/lab03/ directory.
  • lab03.py contains a working factorial function.
  • lab03.py contains a working coin counter function.

Prepared for Computer Science 8 by Ziad Matni, partly based on an idea by Diana Franklin and Michael Costanzo.