473,699 Members | 2,534 Online

# 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"
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 ^_^
Jul 18 '05 #1
4 1289
"R.Meijer" <mi********@gma il.com> wrote in message
news:ba******** *************** ***@posting.goo gle.com...
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"
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:

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

Jul 18 '05 #2
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:

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?
Jul 18 '05 #3
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
Jul 18 '05 #4
"R.Meijer" <mi********@gma il.com> wrote in message
news:lo******** **************@ post.gmane.org. ..
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.

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

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 9 2643 by: Bartosz Wegrzyn | last post by: I need help with sessions. I createt set of web site for nav with authorization. first I go into main.php which looks like this: function doWord(file) { if (navigator.userAgent.indexOf("MSIE")!=-1) 5 1840 by: tin | last post by: