On 10/22/07, Py-Fun <[EMAIL PROTECTED]> wrote: > On 22 Oct, 13:28, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > > Py-Fun wrote: > > > I'm stuck trying to write a function that generates a factorial of a > > > number using iteration and not recursion. Any simple ideas would be > > > appreciated. > > > > Show us your attempts, and we might suggest a fix. Because otherwise this > > sounds suspiciously like homework. > > > > Diez > > Here is my futile attempt. Be careful with this though, I just ran > something similar and it was never ending... > > def itforfact(n): > while n<100: > print n > n+1 > n = input("Please enter a number below 100") > > itforfact(n)
Let me give you a pseudo code (which though can be found in most of the textbooks and some 'simple' googling). Try to understand the code and then write an equivalent python function. function iFactorial(n: integer): integer; var i, temp: integer; begin temp = 1; for i = 1 to n do temp = temp * i end return temp About your code. 1. why doesn't it surprise you if the code that you posted goes in infinite loop ?! 2. why do you use condition: n < 100 3. How do you think that your function will calculate the factorial ? 4. Instead of "input" use "raw_input", and then "cast" the input as integer . Cheers, amit. -- -- Amit Khemka -- http://mail.python.org/mailman/listinfo/python-list