-1

I want to handle ValueError in Python. But whenever I type the below code I get an error and I want the output as I excepted.

a, b = map(int, input().split())
try:
    print(a//b)
except ZeroDivisionError as e:
    print('Enter code: ', e)
except ValueError as e:
    print('Enter code: ', e)

If my inputs for a and b are '1' and '$' than Expected output for ValueError: 'Enter code: invalid literal for int() with base 10: $

azro
  • 53,056
  • 7
  • 34
  • 70
Sharma Shivlal
  • 21
  • 1
  • 1
  • 7
  • What is your question? – AMC Mar 20 '20 at 18:51
  • Does this answer your question? [ValueError: invalid literal for int() with base 10: ''](https://stackoverflow.com/questions/1841565/valueerror-invalid-literal-for-int-with-base-10) – AMC Mar 20 '20 at 18:52

1 Answers1

2

Your problem is before your try, here a, b = map(int, input().split())

The $ is given to int, this fails and raise the invalid literal for int() with base 10: $ which is pretty explicit

try:
    a, b = map(int, input().split())
    print(a//b)
except ZeroDivisionError as e:
    print('Enter code: ', e)
except ValueError as e:
    print('Enter code: ', e)
azro
  • 53,056
  • 7
  • 34
  • 70