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 assert statement 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 arg2have 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.

MY CODE:

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 parts = string.split(' ') if parts[0] not in ['add','sub','mul','div']: raise InvalidCommand else: if parts[1].isdigit() and parts[2].isdigit(): parts[1], parts[2] = float(parts[1]), float(parts[2]) return (parts[0],[parts[1],parts[2]]) else: raise InvalidCommand # Test Code print(validate_input('add 4 5')) print(validate_input('sub 5 5')) print(validate_input('mul 9 5')) print(validate_input('div 4 23')) print(validate_input('div f 23')) print(validate_input('gone 4 23'))

there are something wrong:

--------- Test 1 --------- Expected Output: Code Analyser

Test Result: ----- Analysis Errors ----- You need a try/except statement to check if the float conversion works

--------- Test 2 --------- Expected Output: validate_input('add 2 3') -> ('add', [2., 3.])

Test Result: Correct!

--------- Test 3 --------- Expected Output: validate_input('div 78 123') -> ('div', [78., 123.])

Test Result: Correct!

--------- Test 4 --------- Expected Output: validate_input('banana 2 3') -> raises InvalidCommand()

Test Result: Correct!

--------- Test 5 --------- Expected Output: validate_input('add thingy 3') -> raises InvalidCommand()

Test Result: Correct!

--------- Test 6 --------- Expected Output: validate_input('add') -> raises InvalidCommand()

Test Result: list index out of range

--------- Test 7 --------- Expected Output: validate_input('add 2') -> raises InvalidCommand()

Test Result: list index out of range

--------- Test 8 --------- Expected Output: validate_input('add 2 3 4') -> raises InvalidCommand()

Test Result: Your code must raise InvalidCommand()

PLEASE HELP ME SLOVE THIS PORBLEM

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!