[Tutor] Simple factorial program

Eddie eddie9139 at gmail.com
Thu Jun 11 18:20:06 CEST 2009


Thanks guys, I got it working for what I need at the moment with

#Factorial finder

factorialNum = 1L
print "Factorial finder"
number = int(input("Please enter a non-negative integer: "))

for number in range(1, number):
  factorialNum = (factorialNum * (number + 1))
  print factorialNum

print "Factorial:", factorialNum

On this mailing list, is there a general rule about how to place code? I.e.
ident, italics, quotes???

2009/6/12 ayyaz <ayyaz84 at gmail.com>

> Eddie wrote:
>
>> 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
>>      Thanks Eddie
>>
>>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
> Hello Eddie,
>
> You never initialized the variable factorial. So the line
> "factorial = (factorial * number)" will crash your program since
> the program doesn't know what the variable factorial is.
>
> I would modify your code as follows:
>
> factorial  = int(raw_input("\nPlease enter a non-negative integer: "))
>
> for i in range(1,factorial):
>    factorial *= i
>
> print "Factorial:", factorial
>
> raw_input("\nPress enter to exit")
>
>
> I hope this helps.
> --ayyaz
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090612/1eda9137/attachment.htm>


More information about the Tutor mailing list