Newbie Question - Checkboxes

John Machin sjmachin at lexicon.net
Mon Dec 11 00:32:48 EST 2006


Leanne wrote:
> I have been using Python for only a few months part-time and have
> recently had my first encounter with retrieving user input from
> checkboxes.  I have a problem with the way Python reads the input
> values from a set of checkboxes on a webpage.
>
> The values assigned to the checkboxes are integers.  I first of all
> check for a key of CHK_CHILDREN to see if any checkboxes have been
> checked.  That works fine.
>
> I then use a 'for x in CHK_CHILDREN' loop to get all the values from
> the returned list.  This works for most cases except one.  If the user
> only checks 1 checkbox, Python reads a scalar, not a list, and if this
> scalar is a double digit integer (eg 11), then the loop goes through
> each digit and retrieves these values, eg retrieves 1 and then
> retrieves 1 again.
>

You evidently mean that the scalar you are getting is e.g. "11" and
your loop is returning "1" then "1" again. So there are two
possibilities: a string (either str or unicode) or a list (or some
other sequence).

I'd try this:

if isinstance(returned_value, basestring):
    returned_value = [returned_value]
for item in returned_value:
    do_something_with(item)

HTH,
John




More information about the Python-list mailing list