[Tutor] How can I make this run right?

Simon Gerber nequeo at gmail.com
Mon Aug 15 09:48:58 CEST 2005


I'd do it like this... And there's probably an even quicker way, if
you really sat down and thought about it.

n = int(raw_input("Number: "))
x = n-1
while x > 1:
    n *=x
    x -=1
print n

The reason why it isn't working is because of
"
while x > 1:
        x -= 1
"
When your program hits this point it stays in the while loop,
subtracting 1 from x each time, but not multiplying anything anymore,
until it hits break. So your program does this.

Number: 4
n = 5
x = 4
t = 20
x = 3
x = 2
x = 1
BREAK.



Cheers!

On 15/08/05, Nathan Pinno <falcon3166 at hotmail.com> wrote:
>
> The following code is supposed to take in a number, and print number!:
> n = int(raw_input("Number: "))
> x = n-1
> while 1:
>     t = n*x
>     while x > 1:
>         x -= 1
>     else:
>         break
> print t
>
> Why isn't it working, and how can I make it print out the correct output?
>
> Thanks in advance,
> Nathan
> ---------------------------------------------------------------
> Early to bed,
> Early to rise,
> Makes a man healthy, wealthy, and wise.
> --Benjamin Franklin
> -------------------------------------------------------------------
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>
>


More information about the Tutor mailing list