Python

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • How can I make this run right?

    8 answers - 480 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    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.
    Franklin
    Tutor maillist - Tutor (AT) python (DOT) org
  • No.1 | | 1263 bytes | |

    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!

    15/08/05, Nathan Pinno <falcon3166 (AT) hotmail (DOT) comwrote:

    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.
    Franklin

    Tutor maillist - Tutor (AT) python (DOT) org

    >
    >
    >
    >


    Tutor maillist - Tutor (AT) python (DOT) org
  • No.2 | | 656 bytes | |

    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?

    So give is a clue! What is it doing wrong? Is there an error message?
    What output did you expect? What did you get?

    Without that information we have to read the code and guess what you
    were trying to do. Then try to guess what might be different to what
    you expected.

    Alan G.

    Tutor maillist - Tutor (AT) python (DOT) org
  • No.3 | | 656 bytes | |

    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?

    So give is a clue! What is it doing wrong? Is there an error message?
    What output did you expect? What did you get?

    Without that information we have to read the code and guess what you
    were trying to do. Then try to guess what might be different to what
    you expected.

    Alan G.

    Tutor maillist - Tutor (AT) python (DOT) org
  • No.4 | | 763 bytes | |

    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?

    thing you might find handy is IDLE's debugger (if you use IDLE).
    Turn it on under Debug / Debugger. Make sure the "Source" checkbox is
    checked. Then hit the step button to run your program in "slow motion"
    so you can see everything it does one step at a time.

    (You might have to temporarily click the output window to the front
    when the raw_input wants you to type a number)

    Alan

    Tutor maillist - Tutor (AT) python (DOT) org
  • No.5 | | 1252 bytes | |

    I put in a 4 and expected 24, and instead got 12. It is supposed to give 4!
    not 4*3.

    Early to bed,
    Early to rise,
    Makes a man healthy, wealthy, and wise.
    Franklin

    Message
    From: "Alan G" <alan.gauld (AT) freenet (DOT) co.uk>
    To: "Nathan Pinno" <falcon3166 (AT) hotmail (DOT) com>; "Tutor mailing list"
    <tutor (AT) python (DOT) org>
    Sent: Monday, August 15, 2005 2:20 AM
    Subject: Re: [Tutor] How can I make this run right?


    >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?
    >

    So give is a clue! What is it doing wrong? Is there an error message?
    What output did you expect? What did you get?

    Without that information we have to read the code and guess what you
    were trying to do. Then try to guess what might be different to what
    you expected.

    Alan G.

    Tutor maillist - Tutor (AT) python (DOT) org
  • No.6 | | 989 bytes | |

    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.

    Tutor maillist - Tutor (AT) python (DOT) org
  • No.7 | | 622 bytes | |

    Nathan Pinno napsal(a):

    >I put in a 4 and expected 24, and instead got 12. It is supposed to give 4!
    >not 4*3.
    >


    Dear Nathan,

    Everyone is giving you very good hints but seems to me you keep to
    resist :-)
    Looks like you always wait for complete solution, dont you?
    Here it is. There is one add and two changes. Can you see them?

    n = int(raw_input("Number: "))
    x = n-1
    t=n
    while 1:
    t = t*x
    if x 1:
    x -= 1
    else:
    break
    print t

    Its not the best solution for number!, but it is yours (and a bit of
    mine :-) )
  • No.8 | | 795 bytes | |

    >I put in a 4 and expected 24, and instead got 12. It is supposed to
    >give 4!

    not 4*3.

    Thanks Nathan. It would be good to get that kind of detail when you
    post
    the initial message. Your function didn't look like the usual
    factorial
    function.

    FWIW here is my version :-)

    def factorial(n):
    result = 1
    for x in range(2,n+1):
    result *= x
    return result

    By avoiding while loops you avoid all the mess of trying to maintain
    the counter - one of the most common causes of errors in programming!

    For a different approach see the recursion topic in my tutor.

    HTH,

    Alan G
    Author of the Learn to Program web tutor

    Tutor maillist - Tutor (AT) python (DOT) org

Re: How can I make this run right?


max 4000 letters.
Your nickname that display:
In order to stop the spam: 3 + 3 =
QUESTION ON "Python"

EMSDN.COM