hello,

small question if i may :-)

try:
   x=int(input())
except ValueError as var:
   print(str(var.args[0]))

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

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

''' possible result (Python3) ...
invalid literal for int() with base 10: 'abcd'
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()
try:
    x=int(inString)
except ValueError as var:
    print(inString)

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

x=int(input())

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.