How to implicitly check method arguments in Python?

Question about Newton Method's modification in Python?

  • How do you Modify the program to allow the user to specify the minimum level of precision in the answer to between 1 and 10 digits to the right of the decimal place. So Far I have: print "This program will compute the square root using Newton's guess and check method" print n=input("Please input the value you wish to find the square root of ") reps=input("Please input the number of times you wish to improve the guess: ") firstguess=n/2.0 nextguess=0 for i in range(reps): nextguess=firstguess firstguess=((firstguess+(n/firstguess)))… print nextguess

  • Answer:

    instead of using a for loop for a given number of repetitions, use a while loop and specify a tolerance. something like this: userTol = input("Please enter a tolerance: ") # Number of digits userTol = float('1E-'+str(userTol)) # Convert input to tolerance setting tol = 2*userTol # create a bigger value than userTol while (tol > userTol): # Newton code here tol = abs(firstguess - nextguess)

Ajantha R at Yahoo! Answers Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.