how best to check a value? (if/else or try/except?)

John McMonagle jmcmonagle at velseis.com.au
Thu Jul 27 18:05:27 EDT 2006


On Thu, 2006-07-27 at 20:44 +0000, John Salerno wrote:
> My code is below. The main focus would be on the OnStart method. I want 
> to make sure that a positive integer is entered in the input box. At 
> first I tried an if/else clause, then switched to try/except. Neither is 
> perfect yet, but I was wondering which I should try for in the first 
> place. I figure I need to check for an emptry string, non-numeric 
> strings (maybe these are the same check), 0 and negative numbers (which 
> might also fall into the category of 'anything but a number' because of 
> the negative sign).
> 

Have a look at using a wx.Validator in your wx.TextCtrl.


Syntax for wx.TextCtrl:

wx.TextCtrl(parent, id, value="", pos=wx.DefaultPosition,
size=wx.DefaultSize, style=0, validator=wx.DefaultValidator,
name=wx.TextCtrlNameStr)

This will enable you to automagically check the TextCtrl if it's empty,
has wrong data type, does not meet a prescribed condition (eg: no
negative numbers) and do something if it does not pass validation (eg:
colour the background of the TextCtrl red, initiate a system beep, etc).

Regards,

John



-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.




More information about the Python-list mailing list