[Tutor] how to use int and split() simultaneously

Wayne Werner waynejwerner at gmail.com
Thu Dec 8 19:57:26 CET 2011


On Thu, Dec 8, 2011 at 7:43 AM, Dario Lopez-Kästen <cl2dlope at gmail.com>wrote:

> <snip>
>
> In that case a for loop with a try-except would better:
>
> strNum = raw_input("enter numbers, separated by space: ").split()
> num_list = []
>
> for token in strNum:
>     try:
>         num_list.append(int(token))
>     except ValueError:
>         # Do nothing, fetch the next token
>         continue
>
> print repr(num_list)
> <snip>
> Strictly speaking the "continue" in the except clause is not necessary in
> this simple example.
>

Though you do have to have *something* or you'll get a syntax error because
you can't have an empty block. The keyword `pass` works well in cases like
this.

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111208/6a1a4646/attachment.html>


More information about the Tutor mailing list