Question: Raising an Exception In the previous problem, you used a try/except statement to catch an exception. This problem deals with the opposite situation: raising an

Raising an Exception

In the previous problem, you used a try/except statement to catch an exception. This problem deals with the opposite situation: raising an exception in the first place.

One common situation in which you will want to raise an exception is where you need to indicate that some precondition that your code relies upon has not been met. (You may also see the assertstatement used for this purpose, but we wont cover that here.)

Write a function validate_input(string) which takes a command string in the format 'command arg1 arg2' and returns the pair ('command', [arg1, arg2]), where arg1 and arg2 have been converted to floats. If the command is not one of 'add', 'sub', 'mul', or 'div', it must raise InvalidCommand. If the arguments cannot be converted to floats, it must raise InvalidCommand.

class InvalidCommand(Exception): pass

def validate_input(string): """ If string is a valid command, return its name and arguments. If string is not a valid command, raise InvalidCommand

Valid commands: add x y sub x y mul x y div x y

Parameters: string(str): a valid command (see above)

Return: tuple>: the command and its corresponding arguements

Precondition: Arguments x and y must be convertable to float. """ # your code here

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!