Jun-19-2021, 12:52 AM
The main program is responsible for input, but you don't validate that input until it's handed to the calculation function. So your choices at that point are to:
* Raise an exception and have the main program handle the exception
* Return data that is a signal to the main program that something is broken
* Have the calculation function ask for input until corrected
* Return invalid data
To me, the first one is the cleanest. Just let the division create an exception. But have the main program handle the exception and ask the user to re-enter the data. In fact, you're already looping and asking for data again anyway. So just handle the exception and tell the user you can't do that.
Something like:
* Raise an exception and have the main program handle the exception
* Return data that is a signal to the main program that something is broken
* Have the calculation function ask for input until corrected
* Return invalid data
To me, the first one is the cleanest. Just let the division create an exception. But have the main program handle the exception and ask the user to re-enter the data. In fact, you're already looping and asking for data again anyway. So just handle the exception and tell the user you can't do that.
Something like:
... elif operat == '/': try: result = division(num,num2) print(f"The result of calculation is :{result}") except ZeroDivisionError: print("Cannot divide by zero.")