0

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 by Drau: n/a

3
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by Drau
Featured Replies
  • 1

    [code]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 ''' [/code] Read More

  • 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: [code] inString = input() try: x=int(inString) except ValueError as var: print(inString)[/code] Read More

1
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
'''
0

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

weird !

1

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 by Schol-R-LEA: n/a

0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.