Noob question: Is all this typecasting normal?

vk vminch at
Fri Jan 2 23:36:04 CET 2009

> You might better do
> bet = int(raw_input("Enter your bet"))
> because then you don't need to later on convert bet again and again.

This is all fine until you give it to an end-user.
This is what I picture:

$ ./
Enter your bet: $10

.. or perhaps "ten", "all", or a jillion other tainted inputs.

Python will try to cast these strings, but will slap you with a
ValueError instead (an error of some sort, at least).

There needs to be a "user_io" or "sanitize" module in the standard
library to take care of this stuff.

import userio

logic = userio.userio()

number = logic.getNumeric("blah: ") # will offer the user a "re-do" in
case of bad input
number = logic.forceGetNumeric("Enter your bet!: ") # even if input is
tainted, will return some number

text = logic.getText("blargh: ") # return all text

text = logic.setValidText("[A-Za-z]")
text = logic.forceGetText("blargh: ") # return some text, strips
invalid chars

... but there isn't, as far as I know.

More information about the Python-list mailing list