I just want to give you a short overview about how to improve your code by using Python's built-in exceptions.
There are several built-in exceptions which will be raised automatically when doing faulty operations. So there is ZeroDivisionError which is raised when dividing by zero. You can catch any of these exceptions by using try-except-blocks.
I am using those built-in exceptions in order to tell the user if his input was invalid or if he decided to divide by zero.
For doing this I rewrote your code as shown below:
def prompt(): num1 = input('Please input a number: ') try: num1 = float(num1) except Exception as e: print('Your input is invalid. Starting at first input again.') return prompt() num2 = input('Please input another number: ') try: num2 = float(num2) except Exception as e: print('Your input is invalid. Starting at first input again.') return prompt() return num1, num2 def divide(): num1, num2 = prompt() result = None msg = None try: result = num1 / num2 except ZeroDivisionError as e: msg = str(e) result = None return msg, result msg, result = divide() if result: print('The result of you calculation is: {}'.format(result)) else: print('The following error occured: {}'.format(msg))
You can still do some further improvements or shorten my code, for sure. However, I decided to keep this code in this state since I do not want to confuse you too much at this point.
Noneif nothing explicit is returned.