Wrong with this script?

Discussion in 'Python' started by R.Meijer, Mar 5, 2005.

  1. R.Meijer

    R.Meijer Guest

    Hi, I've been busy with an experimental script, and I can't seem to
    see what is wrong with it, can somebody tell me?

    Here it is:

    a = 0
    b = 1
    mainloop = 1

    print "Welcome to pyFibo"
    print "For more information type \'help\'"
    while mainloop==1:
    limit = input("Until what number do you want to see the
    Fibonacci series?")
    if limit=="help":
    print "The Fibonacci series is a worldfamous series of
    numbers.\
    Each consecutive number is calculated by adding the previous two
    numbers to\
    each other."
    else:
    while b < limit:
    print b
    a, b = b, a+b
    print "Want to do another series?"
    again = input("(Type yes for another series, or anything
    else to quit.)"
    if again!="yes":
    mainloop = 0



    Any help is appreciated, thanks ^_^
     
    R.Meijer, Mar 5, 2005
    #1
    1. Advertising

  2. "R.Meijer" <> wrote in message
    news:...
    > Hi, I've been busy with an experimental script, and I can't seem to
    > see what is wrong with it, can somebody tell me?
    >
    > Here it is:
    >
    > a = 0
    > b = 1
    > mainloop = 1
    >
    > print "Welcome to pyFibo"
    > print "For more information type \'help\'"
    > while mainloop==1:
    > limit = input("Until what number do you want to see the
    > Fibonacci series?")
    > if limit=="help":
    > print "The Fibonacci series is a worldfamous series of
    > numbers.\
    > Each consecutive number is calculated by adding the previous two
    > numbers to\
    > each other."
    > else:
    > while b < limit:
    > print b
    > a, b = b, a+b
    > print "Want to do another series?"
    > again = input("(Type yes for another series, or anything
    > else to quit.)"


    You need to close the () for input here. After doing that, if you run it
    you will notice that you get an exception for most inputs, including "yes".
    IIRC, input() is scheduled for removal in some future version of Python
    because it doesn't do what you would expect and it is generally a bad idea
    to use it. The functionality is along the lines of:

    eval(raw_input('your string here'))

    You undoubtedly want raw_input() instead here.

    > if again!="yes":
    > mainloop = 0


    This last line needs indented.

    And a couple of minor points:

    1. Choose an amount of indentation per level and stick to it. 4 is rather
    common in Python code.

    2. When posting to the list, make sure that the lines in your code are short
    enough that they will not wrap and be posted as broken code. 70 chars is
    usually safe.

    Daniel Fackrell
     
    Daniel Fackrell, Mar 5, 2005
    #2
    1. Advertising

  3. R.Meijer

    R.Meijer Guest

    Daniel Fackrell <unlearned <at> gmail.com> writes:

    >
    > You need to close the () for input here. After doing that, if you run it
    > you will notice that you get an exception for most inputs, including "yes".
    > IIRC, input() is scheduled for removal in some future version of Python
    > because it doesn't do what you would expect and it is generally a bad idea
    > to use it. The functionality is along the lines of:
    >
    > eval(raw_input('your string here'))
    >
    > You undoubtedly want raw_input() instead here.
    >
    > > if again!="yes":
    > > mainloop = 0

    >
    > This last line needs indented.
    >
    > And a couple of minor points:
    >
    > 1. Choose an amount of indentation per level and stick to it. 4 is rather
    > common in Python code.
    >
    > 2. When posting to the list, make sure that the lines in your code are short
    > enough that they will not wrap and be posted as broken code. 70 chars is
    > usually safe.
    >
    > Daniel Fackrell
    >


    Thank you very much for the help and the tips :) This is my very first python
    script, and I knew it would have some stupid mistakes; but it's doing something
    weird right now...I did all the stuff you told, me, and now it'll at least run.
    But whenI enter a number as a limit, the loop keeps going on forever, nad the
    numbers won't stop rolling. I'm guessing this is because it sees limit as a
    string, how can I let it see it as an integer?
     
    R.Meijer, Mar 5, 2005
    #3
  4. R.Meijer wrote:
    > Hi, I've been busy with an experimental script, and I can't seem to
    > see what is wrong with it, can somebody tell me?


    For future notice, it's useful to

    (1) explain what it is you want your script to do, and
    (2) explain what it currently does (including an exception traceback if
    one is printed)

    Using my mind-reading powers, I'd suggest that maybe you want something
    like:

    py> for limit in iter(lambda: raw_input('What number? '), ''):
    .... if limit == "help":
    .... print "The Fibonacci series..."
    .... else:
    .... a, b = 0, 1
    .... limit = int(limit)
    .... while b < limit:
    .... print b
    .... a, b = b, a+b
    ....
    [... I type '6' ...]
    1
    1
    2
    3
    5
    [... I type '13' ...]
    1
    1
    2
    3
    5
    8
    [... I type '' (nothing) ...]
    py>

    STeVe
     
    Steven Bethard, Mar 5, 2005
    #4
  5. "R.Meijer" <> wrote in message
    news:...
    > Thank you very much for the help and the tips :) This is my very first

    python
    > script, and I knew it would have some stupid mistakes; but it's doing

    something
    > weird right now...I did all the stuff you told, me, and now it'll at least

    run.
    > But whenI enter a number as a limit, the loop keeps going on forever, nad

    the
    > numbers won't stop rolling. I'm guessing this is because it sees limit as

    a
    > string, how can I let it see it as an integer?


    My mistake. I was only looking at the last input() call you were using.
    For the other one, when you change it to raw_input(), you will get a string
    that you must convert to an integer in order to use it for numerical
    calculations or comparisons.

    int(raw_input('your message here'))

    will do this.

    After you make this change, try entering a string that cannot be parsed as
    an integer, and you will see another exception (ValueError) is raised. In
    order to properly handle this, I would wrap the int(raw_input()) in a try:
    except: block inside a loop. When you get a valid integer, you can then
    "break" out of the loop and continue executing.

    You may also want to look at the rest of your script for another place you
    can use "break" in order to eliminate a flag.

    Happy scripting, and welcome to the bliss that is Python.

    Daniel Fackrell
     
    Daniel Fackrell, Mar 5, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Miguel Dias Moura
    Replies:
    4
    Views:
    3,336
    Miguel Dias Moura
    Jun 23, 2004
  2. Matthew
    Replies:
    7
    Views:
    875
    Priscilla Walmsley
    Jan 7, 2005
  3. Rajat
    Replies:
    3
    Views:
    747
    Jorgen Grahn
    Jan 8, 2010
  4. VYAS ASHISH M-NTB837
    Replies:
    2
    Views:
    603
    Jan Kaliszewski
    Jan 7, 2010
  5. kath
    Replies:
    4
    Views:
    814
    J. Gleixner
    Apr 9, 2007
Loading...

Share This Page