newton' method except zerodivisionerrorprint "errorderivative zero for " sys exit( abort with error f_value (xiteration_counter + hereeither solution is foundor too many iterations if abs(f_valueepsiteration_counter - return xiteration_counter def ( )return ** def dfdx( )return * solutionno_iterations newton(fdfdxx= eps= - if no_iterations solution found print "number of function calls% ( *no_iterationsprint " solution is% (solutionelseprint "solution not found!handling of the potential division by zero is done by try-except construction python tries to run the code in the try block if anything goes wrong hereor more preciselyif python raises an exception caused by problem (such as division by zeroarray index out of boundsuse of undefined variableetc )the execution jumps immediately to the except block herethe programmer can take appropriate actions in the present casewe simply stop the program (professional programmers would avoid calling sys exit inside function insteadthey would raise new exception with an informative error messageand let the calling code have another try-except construction to stop the program the division by zero will always be detected and the program will be stopped the main purpose of our way of treating the division by zero is to give the user more informative error message and stop the program in gentler way calling sys exit with an argument different from zero (here signifies that the program stopped because of an error it is good habit to supply the value because tools in the operating system can then be used by other programs to detect that our program failed to prevent an infinite loop because of divergent iterationswe have introduced the integer variable iteration_counter to count the number of iterations in newton' method with iteration_counter we can easily extend the condition in the while such that no more iterations take place when the number of iterations reaches we could easily let this limit be an argument to the function rather than fixed constant the newton function returns the approximate solution and the number of iterations the latter equals if the convergence criterion jf / was not reached within the maximum number of iterations in the calling codewe print out the