code won't run, that is for sure. But even if your syntax is 100% correct and runs ne, there could be cases when you get errors during the runtime of your program. These errors that get triggered in the runtime are called Excep ons.
two numbers, both received from the user input. a = float(input('First Number: ')) b = float(input('Second Number: ')) result = a / b It would raise a runtime error ZeroDivisionError and the program would halt.
Handling exceptions ensures that the program still continues to run regardless of the exceptions. a = float(input('First Number: ')) b = float(input('Second Number: ')) try: result = a / b except ZeroDivisionError: print('Error: Division by Zero')
= float(input('Second Number: ')) try: result = a / b print('Result = {}'.format(result)) except ZeroDivisionError: print('Error: Division by Zero') try_again = input('\nTry Again (Y/N)? ') # If the user doesn't want to try again exit the loop. if try_again.upper() != 'Y': break print() # Program will exit finally print('Good Bye!')
are executed. If any statement causes exceptions the rest of the code in the block are skipped. If the raised exception is there in the except clause, then that particular except block is executed. In case the raised exception is not in the except clause it will propagate to the higher level. If it couldn't nd any handlers even after reaching the highest level, the program terminates with that exception. If no exception occurs inside the try block, the except blocks are skipped.
previous lesson. n, sum = 0, 0 while n < 5: value = input('Enter Number %s: ' % (n + 1)) sum = sum + float(value) n += 1 print('Sum = %.2f' % sum) This program expects numeric values from the user.
input like abx , xyz etc. You'll get an error like this: Traceback (most recent call last): File "units/python/4/example_5.py", line 7, in <module> sum = sum + float(value) ValueError: could not convert string to float: 'abc'
now. n, sum = 0, 0 while n < 5: value = input('Enter Number %s: ' % (n + 1)) try: value = float(value) sum = sum + value n += 1 except ValueError: print('Invalid Input. Please enter a numeric value.\n') print('\nSum = %.2f' % sum)
raise exceptions like this: raise Exception('Hey, this was a test exception.') raise ValueError('Hey, this was another exception.') The only argument required for the raise keyword is the exception itself. This could be either an exception instance or exception class(a class that derives from Exception).
the age of the user by checking his date of birth. - Refactor it using functions. - Handle runtime exceptions. - Ability to try again in case of invalid input.
value of m and c from the equa on of line y = mx + c - Refactor the logic for parsing the equation to a function - Take two user inputs: equation of two lines - Write a function to get the intersection of two lines - Write a function to get the angle between two lines - Print angle between two lines and the point of intersection - Print if they're parallel or perpendicular to each other - Handle runtime exceptions. - Ability to try again in case of invalid input.