[Tutor] How can I make this run right?

luke rabidpoobear at gmail.com
Mon Aug 15 19:59:38 CEST 2005


> I put in a 4 and expected 24, and instead got 12. It is supposed to give
4!
> not 4*3.
[snip rest of message]

nathan
your problem is easy to see...
> >> n = int(raw_input("Number: "))
> >> x = n-1
> >> while 1:
> >>    t = n*x
> >>    while x > 1:
> >>        x -= 1
> >>    else:
> >>        break
> >> print t

think about it for a *while*...
Hint *while*
hint hint *change the while*
hint hint hint *the inner while*

nathan I see that you want to do n!(factorial)
but when you explained it you just said you wanted number!
which is confusing because I think everyone thought you were just excited.
you could always use a recursive function for factorial, IE.

def factorial(n):
 if n > 1:
  return factorial(n-1) * n
 else:
  return n

HTH,
Luke
P.S. Try to fix yours it'll help you.  tell me if you get it working
correctly.



More information about the Tutor mailing list