# Test file
# ECS 10, May 11, 2009
# function to compute n!
# parameter: n, must be non-negative integer
# returns: n! on success
# -1 on failure (bad parameter)
def fact(n):
# check for error in call
if n < 0 or n != int(n):
return -1
# now compute n! by repeated multiplication
fact = 1
for i in range(1, n+1):
fact *= i
# return it
return fact
# Main routine to enable us to test fact()
def main():
# get the input
n = input("[[WARNING: numbers only!]] n: ")
# loop until quit
while n >= 0:
# say what you got so user can check
print "%d! = %d" % (n, fact(n))
# get another input
n = input("[[WARNING: numbers only!]] n: ")
# all done! say what user typed
print "Quitting: n =", n
main()