Wrong with this script?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • R.Meijer

    #1

    Wrong with this script?

    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 ^_^
  • Daniel Fackrell

    #2
    Re: Wrong with this script?

    "R.Meijer" <misthunter@gma il.com> wrote in message
    news:baa5586e.0 503051223.74bf6 59f@posting.goo gle.com...[color=blue]
    > 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.)"[/color]

    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.
    [color=blue]
    > if again!="yes":
    > mainloop = 0[/color]

    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



    Comment

    • R.Meijer

      #3
      Re: Wrong with this script?

      Daniel Fackrell <unlearned <at> gmail.com> writes:
      [color=blue]
      >
      > 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.
      >[color=green]
      > > if again!="yes":
      > > mainloop = 0[/color]
      >
      > 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
      >[/color]

      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?


      Comment

      • Steven Bethard

        #4
        Re: Wrong with this script?

        R.Meijer wrote:[color=blue]
        > 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?[/color]

        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

        Comment

        • Daniel Fackrell

          #5
          Re: Wrong with this script?

          "R.Meijer" <misthunter@gma il.com> wrote in message
          news:loom.20050 305T214312-587@post.gmane. org...[color=blue]
          > Thank you very much for the help and the tips :-) This is my very first[/color]
          python[color=blue]
          > script, and I knew it would have some stupid mistakes; but it's doing[/color]
          something[color=blue]
          > weird right now...I did all the stuff you told, me, and now it'll at least[/color]
          run.[color=blue]
          > But whenI enter a number as a limit, the loop keeps going on forever, nad[/color]
          the[color=blue]
          > numbers won't stop rolling. I'm guessing this is because it sees limit as[/color]
          a[color=blue]
          > string, how can I let it see it as an integer?[/color]

          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



          Comment

          Working...