[Tutor] Simple factorial program

Emile van Sebille emile at fenx.com
Thu Jun 11 20:01:56 CEST 2009


On 6/11/2009 8:53 AM Eddie said...
> I'm trying to write a simple factorial program and am unsure at what is 
> wrong with it. Why Can't I go *factorial = factorial * number* where 
> factorial and number are both integers?
> 
> #Factorial program
> 
> print "Factorial finder"
> number = input("Please enter a non-negative integer: "
>     for number in range(number, 1)
>     factorial = (factorial * number)
> 
> print "Factorial:", factorial

Another way to approach this is using reduce:

from operator import mul
number = input("Please enter a non-negative integer: ")
factorial = reduce(mul,range(1,number+1),1)

Emile



More information about the Tutor mailing list