[sf-lug] ThinkPython: coding problem
Tom Haddon
tom at greenleaftech.net
Sun Oct 12 12:45:42 PDT 2008
On Sat, 2008-10-11 at 20:59 -0700, Alex Kleider wrote:
> Thanks to Jeff and Jim.
> I googled python exceptions
> and solved my problem using the
> try:
> pass
> except:
> pass
> construct and everything works just fine.
Just a quick note - I'd recommend against ever using "except" on its own
without the specific error you're expecting. In this case, if n is a
string, for example, you'll get a ValueError, so it would be better to
do:
try:
value = int(n)
except ValueError:
return False
The reason you don't want an unqualified "except" is that you may not be
catching the error you thing you're catching without it.
Cheers, Tom
>
> fyi:
>
> def int_check(n):
> try:
> value=int(n)
> except:
> return False
> if type(value)==int:
> return True
> else:
> return False
>
> def get (check, prompt, moan):
> response=raw_input(prompt)
> if check(response):
> return int(response)
> else:
> print moan
> return get (check,prompt,moan)
>
> # main body
> preamble()
> a=get(int_check,'Enter a: ','Problem! a must be an integer. Try again')
>
>
>
>
>
> _______________________________________________
> sf-lug mailing list
> sf-lug at linuxmafia.com
> http://linuxmafia.com/mailman/listinfo/sf-lug
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part
URL: <http://linuxmafia.com/pipermail/sf-lug/attachments/20081012/16518ea8/attachment.pgp>
More information about the sf-lug
mailing list