1

I started learning python last week and I'm unable to get what's wrong here:

def add(x,y):
    """Adds 2 numbers and returns the result"""
    return x+y

def sub(x,y):
    """Subtracts 2 numbers and returns the result"""
    return x-y


a = int(input("Enter first number"))
b = int(input("Enter second number"))
c = int(input("Enter 1 for subtraction , 2 for addition and 3 for both"))
try:
    if c>3:
        raise ValueError()
except ValueError():
    print ("Wrong choice")
else:
    print ("Your choice is ",c)
if (c==1):
    print ("Subtraction of 2 numbers=",(sub(a,b)))
if (c==2):
    print ("Addition of 2 numbers = ",(add(a,b)))
if (c==3):
    print ("Subtraction of 2 numbers=",(sub(a,b)))
    print ("Addition of 2 numbers = ",(add(a,b)))

If I enter 4 it throws this error:

Traceback (most recent call last):
  File "C:/Program Files (x86)/Python35-32/calculator.py", line 15, in <module>
    raise ValueError()
ValueError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Program Files (x86)/Python35-32/calculator.py", line 16, in <module>
    except ValueError():
TypeError: catching classes that do not inherit from BaseException is not allowed

1 Answer 1

8

You are trying to catch an instance of ValueError(), where Python expects you to filter on the type. Remove the call:

except ValueError:
    print ("Wrong choice")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.