Python retrieve string input from int(input()) exception -
Python retrieve string input from int(input()) exception -
i'm trying create next code work:
try: x = int(input()) except valueerror var: #print input of user
if seek print(var)
print error line , not original input of user. example, if user insert bla
instead of integer print bla
p.s must not alter line x = int(input())
, else would've solved
i alter x = int(input())
line, since ask, here ugly hack wich exploits format of valueerror
message:
invalid literal int() base of operations 10: 'foobar'
by splitting @ first :
, removing surrounding '
:
try: x = int(input()) except valueerror e: original_input = str(e).split(":")[1].strip()[1:-1] print(original_input)
by way, if still using python 2.x, should utilize raw_input
instead of input
. in fact old input
automatically effort conversion int
if possible:
try: x = int(raw_input()) except valueerror e: original_input = str(e).split(":")[1].strip()[1:-1] print original_input
python python-3.x
Comments
Post a Comment