Wrong with this script?

Daniel Fackrell unlearned at gmail.com
Sat Mar 5 18:45:16 EST 2005


"R.Meijer" <misthunter at gmail.com> wrote in message
news:loom.20050305T214312-587 at 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.

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






More information about the Python-list mailing list