small question if i may :-)

except ValueError as var:

if i input a string like - abcd
this code prints me the full error message
invalid literal for int() with base 10: 'abcd'
while i need only the input - abcd to be printed/extracted from there

now ,except going over the string of the error message and extracting the
expresion, is there a way to just get the inputed value back?

thx for your help :)

Edited 5 Years Ago by Drau: n/a

    x = int(input())
except ValueError as err:
    # split at ':', strip white space then "'"
    err2 = str(err).split(':')[1].strip().strip("'")

''' possible result (Python3) ...
invalid literal for int() with base 10: 'abcd'

Thank you :)
so i get there is no way around having to deal with a string...

weird !

Could you explain a little more about what you're trying to do? There may be some other ways to solve this problem.

If what you need is the input string, and not the error message, then this should work:

inString = input()
except ValueError as var:

Edited 5 Years Ago by Schol-R-LEA: n/a

true , this is a great solution :)
problem is that my assignment forces me to use this line


then if the input isnt int -ill get a ValueError exception-
and i need to work with the value entered
for that i need to exctact it from the error message string(if there is no easier way)

This article has been dead for over six months. Start a new discussion instead.