[Tutor] Checking for int only

Alan Gauld alan.gauld at blueyonder.co.uk
Mon Jun 28 18:58:28 EDT 2004

> How do you test for something to make sure that it is an int 
> and not anything else so that when you ask them to enter a 
> number they can only enter a number and not any other character.

One of the Python mantras is that its better to ask forgiveness 
than permisssion.

Try converting it to an int() if that doesn't work get them 
to try again:

  val = int(raw_input(....))
except ValueError:
  print "Couldn't read that as a number, try again...


Alan G

More information about the Tutor mailing list