HW 2
Loops and Functions
Introduction to Programming -- CS 140
Spring, 2008
This assignment is due on Friday, February 29
Part one. Debugging
We will start by working on your debugging skills. Here are 3 programs. For each
find what is wrong and how to fix it,just as you did in HW1.
- Program1: This asks the user for a number and then
counts by twos until it gets to that number. Sadly, on some input it never
halts. Find such an input and say how to make the program always halt. Remember
that the <Ctrl-C> combination will stop a runaway program.
- Program 2: This one tries to read and print strings
until it gets an empty string -- one of length 0. This one stops running before
I get a chance to enter any string.
- Program 3: This one reads a number n and prints
the sum of the numbers from 1 to n. Sadly, it gets the answers wrong.
Part two. Your program. For this assignment yoou are to write
one program. You will write the program in stages, the way most programs are
written.
- Write a program that asks the user for numbers over and over until it gets
the number 0. You don't need to do anything with the number at this stage;
just have a loop that reads input over and over until the input is 0.
- You have probably seen the factorial function in math classes. The factorial
of a number n, written n!, is the product of all numbers between 1 and n.
So 2! is 1*2 or 2, 3! is 1*2*3 or 6, and 5! is 1*2*3*4*5, or 120. Write a
function
def
factorial(n)
that takes argument n and returns the factorial of n. You can do
this in a loop that runs through the numbers 2 to n and multiplies a product
variable times each of these numbers. Then change your program so that each
time you get a new value of variable x you print the result given by factorial(x).
At this point your program will work something like this:
Enter a number, or 0 to exit: 5
The factorial of
5 is 120
Enter a number, or 0 to exit: 10
The factorial of
10 is 3628800
Enter a number, or 0 to exit: 0
goodbye!
Note that you don't want to print the factorial of 0; if you want to print
anything at this point, just give an exit message such as "goodbye!"
- Python, unlike most programming languages, has no limit to the size of the
integers it can represent. For example, if you asked the factorial program
in Exercise B to give you 20! it will: 20! = 2432902008176640000. Numbers
like this are hard to make sense of. For this exercise you are to modify your
program to count the number of digits in each answer. There are two ways you
might do this. One is to have a loop that counts how many times the number
can be divided by 10 until it gets to 0. Another way is to convert the number
into a string (the str function will do this) and ask for the length of the
string. One way or another you need to find this length, and add a line to
your program to print it.
After you make this change, here is a typical run of your program:
The program computes
factorials.
Enter a number, or 0 to exit: 5
The factorial of
5 is 120
This number has 3 digits.
Enter a number, or 0 to exit: 10
The factorial of
10 is 3628800
This number has 7 digits.
Enter a number, or 0 to exit: 20
The factorial of 20 is 2432902008176640000
This number has 19 digits
Enter a number, or 0 to exit: 0
goodbye!
Since the different versions of this build on each other,
it is only necessary to hand in the final version of this program. It is due
by midnight at the end of Friday, February 29.