Avoiding "invalid literal for int()" exception

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Mon Dec 11 06:25:10 EST 2006


In <1165832540.392387.240550 at j72g2000cwa.googlegroups.com>, aine_canby
wrote:

> elif uniList[0].lower() in ("p","pass"):
> 	break
> elif int(uniList[0]) in range(0,10):

Replace this line with:

elif uniList[0].isdigit() and 0 <= int(uniList[0]) < 10:

>         verb.SetImportance(int(uniList[0]))
>         break
> else:
>         verb.AddNewVerb((uniList[0])

You may want to give `uniList[0]` a name before entering the
``if``/``elif`` construct to avoid accessing the first element over and
over.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list