Avoiding "invalid literal for int()" exception

A

aine_canby

v = raw_input("Enter: ")
Enter: kjjkjTraceback (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.
 
M

Marc 'BlackJack' Rintsch

aine_canby said:
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
 
J

John Machin

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
 
A

aine_canby

John Machin skrev:
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.
 
G

Gabriel Genellina

At said:
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
 
M

Marc 'BlackJack' Rintsch

At said:
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
 
P

Peter Otten

Marc said:
At said:
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:
False

Peter
 
G

Gabriel Genellina

Marc 'BlackJack' Rintsch ha escrito:
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.
 
F

Fredrik Lundh

Gabriel said:
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top