By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,633 Members | 1,478 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,633 IT Pros & Developers. It's quick & easy.

Avoiding "invalid literal for int()" exception

P: n/a
>>v = raw_input("Enter: ")
Enter: kjjkj
>>int(v)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'kjjkj'

In my program I need to be able to enter char strings or int strings on
the command line. Then I use an if-elif structure to establish which is
which. For example -

if uniList[0].lower() in ("e","exit"): # uniList stores a unicode
string origionally taken from stdin
return
elif uniList[0].lower() in ("h","help"):
verb.PrintVerb()
elif uniList[0].lower() in ("p","pass"):
break
elif int(uniList[0]) in range(0,10):
verb.SetImportance(int(uniList[0]))
break
else:
verb.AddNewVerb((uniList[0])

How could I avoid the ValueError exception if uniList[0] == "ker"? I
was thinking of having something like -

formatError = False
try:
iVal = int(uniList[0])
if iVal not in range range(0,10):
formatError = True
catch ValueError:
iVal = -1

if uniList[0].lower() in ("e","exit"): # uniList stores a unicode
string origionally taken from stdin
return
elif uniList[0].lower() in ("h","help"):
verb.PrintVerb()
elif uniList[0].lower() in ("p","pass"):
break
elif iVal != -1 and not formatError:
verb.SetImportance(iVal)
break
elif not formatError:
verb.VerbEntered((uniList[0])
else:
print "Enter numbers between 0 and 10..."

Is this the correct way to do this in your opinion? Sorry, I'm totally
new to python and exceptions.

Thanks,

Aine.

Dec 11 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
In <11**********************@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
Dec 11 '06 #2

P: n/a

ai********@yahoo.com wrote:
>v = raw_input("Enter: ")
Enter: kjjkj
>int(v)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'kjjkj'

In my program I need to be able to enter char strings or int strings on
the command line. Then I use an if-elif structure to establish which is
which. For example -

if uniList[0].lower() in ("e","exit"): # uniList stores a unicode
string origionally taken from stdin
return
elif uniList[0].lower() in ("h","help"):
verb.PrintVerb()
elif uniList[0].lower() in ("p","pass"):
break
elif int(uniList[0]) in range(0,10):
verb.SetImportance(int(uniList[0]))
break
else:
verb.AddNewVerb((uniList[0])

How could I avoid the ValueError exception if uniList[0] == "ker"?I
was thinking of having something like -

formatError = False
try:
iVal = int(uniList[0])
if iVal not in range range(0,10):
formatError = True
catch ValueError:
Perhaps you meant
except ValueError:
:-)

iVal = -1
Consider using uniList[0].isdigit() -- see
http://docs.python.org/lib/string-methods.html

HTH,
John

Dec 11 '06 #3

P: n/a

John Machin skrev:
ai********@yahoo.com wrote:
>>v = raw_input("Enter: ")
Enter: kjjkj
>>int(v)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'kjjkj'

In my program I need to be able to enter char strings or int strings on
the command line. Then I use an if-elif structure to establish which is
which. For example -

if uniList[0].lower() in ("e","exit"): # uniList stores a unicode
string origionally taken from stdin
return
elif uniList[0].lower() in ("h","help"):
verb.PrintVerb()
elif uniList[0].lower() in ("p","pass"):
break
elif int(uniList[0]) in range(0,10):
verb.SetImportance(int(uniList[0]))
break
else:
verb.AddNewVerb((uniList[0])

How could I avoid the ValueError exception if uniList[0] == "ker"? I
was thinking of having something like -

formatError = False
try:
iVal = int(uniList[0])
if iVal not in range range(0,10):
formatError = True
catch ValueError:

Perhaps you meant
except ValueError:
:-)

iVal = -1

Consider using uniList[0].isdigit() -- see
http://docs.python.org/lib/string-methods.html

HTH,
John
Yes, thanks for your help.

Dec 11 '06 #4

P: n/a
At Monday 11/12/2006 07:22, ai********@yahoo.com wrote:
>elif int(uniList[0]) in range(0,10):
Either of these will work to avoid an unneeded conversion:

elif uniList[0] in "0123456789":

elif uniList[0] in string.digits:

elif uniList[0].isdigit():
--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam gratis!
Abr tu cuenta ya! - http://correo.yahoo.com.ar
Dec 11 '06 #5

P: n/a
In <ma***************************************@python. org>, Gabriel
Genellina wrote:
At Monday 11/12/2006 07:22, ai********@yahoo.com wrote:
>>elif int(uniList[0]) in range(0,10):

Either of these will work to avoid an unneeded conversion:

elif uniList[0] in "0123456789":

elif uniList[0] in string.digits:

elif uniList[0].isdigit():
The last does not work. Not only that it accepts numbers greater than 9
because it checks if the whole string consists of digits, it also accepts
u'²₂' and other unicode digits.

Ciao,
Marc 'BlackJack' Rintsch
Dec 12 '06 #6

P: n/a
Marc 'BlackJack' Rintsch wrote:
In <ma***************************************@python. org>, Gabriel
Genellina wrote:
>At Monday 11/12/2006 07:22, ai********@yahoo.com wrote:
>>>elif int(uniList[0]) in range(0,10):

Either of these will work to avoid an unneeded conversion:

elif uniList[0] in "0123456789":

elif uniList[0] in string.digits:

elif uniList[0].isdigit():

The last does not work. Not only that it accepts numbers greater than 9
because it checks if the whole string consists of digits, it also accepts
u'?' and other unicode digits.
By the way, if you require an implicit 0 <= int(s) < 10 check, none of the
above work with the newer Pythons:
>>s = "23"
s in "0123456789"
True
>>s in set("0123456789")
False

Peter
Dec 12 '06 #7

P: n/a
Marc 'BlackJack' Rintsch ha escrito:
In <ma***************************************@python. org>, Gabriel
Genellina wrote:
elif uniList[0].isdigit():

The last does not work. Not only that it accepts numbers greater than 9
because it checks if the whole string consists of digits, it also accepts
u'²₂' and other unicode digits.
Oh, I didn't know that last part! Thanks.
I get a bit confused by the [0], thinking it was checking a single
character.

--
Gabriel Genellina

Dec 12 '06 #8

P: n/a
Gabriel Genellina wrote:
>>elif uniList[0].isdigit():
>The last does not work. Not only that it accepts numbers greater than 9
because it checks if the whole string consists of digits, it also accepts
u'²₂' and other unicode digits.

Oh, I didn't know that last part! Thanks.
I get a bit confused by the [0], thinking it was checking a single
character.
if you really want to use isdigit(), writing

uniList[0].encode("ascii", "ignore").isdigit()

should solve the unicode issue, I think.

</F>

Dec 12 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.