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 :)

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)

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)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.