CS 170 Lab 3: Fun with Functions
This is a lab exercise that has you figuring out how to calculate
the factorial of a number and other functions as well.
- Save the file fact.py to your CS
170 directory.
- Launch Idle and use Open to begin editing this file. As given,
this file asks the user how many rows to print, then prints the
value of several functions for that many rows. You can run it (say with
input 10) to see the results.
- Notice that factorial is always reported to have the value of 1.
That is wrong. Recall that n! (aka n factorial) is 1*2*3*...*n, so,
for example, 3! is 1*2*3 or 6. Your primary task is to update the
factorial function to correctly calculate the factorial value. You should
do this by accumulating the result of the multiplications.
- Extend the program to also calculate the sum of the numbers 1 to n
using a function and then add a call to that function to the table
that is printed. Call your function
sumUp
.
- When your program is working properly (for example, when it
calculates 10! to be 3,628,800 and sumUp(10) to be 55) you should
print out a copy and hand it in. Make sure your printout has your
name and Lab 03 prominently displayed on it.