Question

Please can someone help me correct this code? def newton(num): temp = 0.000001 estimate = 1.0...

Please can someone help me correct this code?

def newton(num):
temp = 0.000001
estimate = 1.0
while True:
estimate = (estimate + num / estimate) / 2
difference = abs(num - estimate ** 2)
if difference <= temp:
break
return estimate
def main():
while True:
try:
num = int(input("Enter a positive number or enter/return to quit: "))
print("newton = %0.15f" % newton(num))
except:
return
main()

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Indentation is very important in python. If you're not following indentation the compiler will give you an error.

Source code:

def newton(num):
temp = 0.000001
estimate = 1.0
while True:
estimate = (estimate + num / estimate) / 2
difference = abs(num - estimate ** 2)
if difference <= temp:
break
return estimate
def main():
while True:
try:
num = int(input("Enter a positive number or enter/return to quit: "))
print("newton = %0.15f" % newton(num))
except:
return
main()

Code in editor:

Expected output:

Add a comment
Know the answer?
Add Answer to:
Please can someone help me correct this code? def newton(num): temp = 0.000001 estimate = 1.0...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can you please enter this python program code into an IPO Chart? import random def menu():...

    Can you please enter this python program code into an IPO Chart? import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the non-numbers #if user enters letters, then except will show the message, Numbers only! try: c=int(input("Enter your choice: ")) if(c>=1 and c<=3): return c else: print("Enter number between 1 and 3 inclusive.") except: #print exception print("Numbers Only!")...

  • i have to decompose newtons square root method in python into three different functions (newton, limitReached, and improveEstimate.) this is what i have to start with.

    import mathdef limitReached(val,last):    return abs(val - last) < 1e-2def improvateEstimate(val,n):    return (val + n / val) * 0.5def squareroot(n):    val = n    while True:        last = val        val = improvateEstimate(val,n)        if limitReached(val,last):            break    return valwhile True:    n=(input(' Enter a positive number or enter/return to quit: '))    if n=='':        break    else:        print('The program\'s estimate is :',squareroot(eval(n)))        print('Python\'s estimate is      :', math.sqrt(eval(n)))        print()

  • Write a python program write a function numDigits(num) which counts the digits in int num. Answer...

    Write a python program write a function numDigits(num) which counts the digits in int num. Answer code is given in the Answer tab (and starting code), which converts num to a str, then uses the len() function to count and return the number of digits. Note that this does NOT work correctly for num < 0. (Try it and see.) Fix this in two different ways, by creating new functions numDigits2(num) and numDigits3(num) that each return the correct number of...

  • CAN YU HELP ME CONSTRUCT A FLOW CHART FOR THE FOLLOW PROGRAM ? C++ CODE: #include<iostream>...

    CAN YU HELP ME CONSTRUCT A FLOW CHART FOR THE FOLLOW PROGRAM ? C++ CODE: #include<iostream> using namespace std; int main() { //declaring variable num int num; //prompting user to enter a number cout<<"Input a number to check prime or not:"; //reading input from user cin>>num; //loop to check user input for positive number while(num<0) { cout<<"Error! Positive Integers Only.\n"; cout<<"Input a number to check prime or not:"; cin>>num; } //initializing isPrime variable with false bool isPrime = true; //loop...

  • Why this C++ code output is 1 1 num is 3 Can u explain to me?...

    Why this C++ code output is 1 1 num is 3 Can u explain to me? Thank u code: #include using namespace std; void xFunction(int num) { do { if (num % 2!= 0) cout << num << " "; num--; } while (num >=1); cout << endl; } int main() { int num = 1; while (num <= 2) { xFunction(num); ++num; } cout << "num is " << num; return 0; }

  • Please, I need help debuuging my code. Sample output is below MINN = 1 MAXX =...

    Please, I need help debuuging my code. Sample output is below MINN = 1 MAXX = 100 #gets an integer def getValidInt(MINN, MAXX):     message = "Enter an integer between" + str(MINN) + "and" + str(MAXX)+ "(inclusive): "#message to ask the user     num = int(input(message))     while num < MINN or num > MAXX:         print("Invalid choice!")         num = int(input(message))     #return result     return num #counts dupes def twoInARow(numbers):     ans = "No duplicates next to each...

  • Can someone help me fill in the first part of this c++ code: I need it...

    Can someone help me fill in the first part of this c++ code: I need it to print the menu based on the day selected. #include #include using namespace std; int main(){ double sub_total=0,tax=0,total=0; string food1[3]={"T-Bone Steak","Pork Chops","Iceland Cod"}; double prices_f1[]={20.50,15.45,10.55}; string food2[3]={"Sirloin Steak","Salmon Fillet","Jumbo Shrimp"}; double prices_f2[]={30.35,24.50,15.50}; string food3[3]={"Pork Tenderloin","Buffalo Chicken Sandwhich","Avocado Burger"}; double prices_f3[]={10.60,15.60,25.60}; string drink1[3]={"Raspberry Sgroppino","Sparkling Apple Sangria ","Spiced Cranberry Rum"}; double prices_d1[]={2.55,5.75,4.25}; string drink2[3]={"Champagne Mojitos","Roman Hoilday Cocktail","Dry Martini"}; double prices_d2[]={1.5,3,25,8.45}; string drink3[3]={"Radler Beer","Port wine","Primm's"}; double prices_d3[]={3.55,2.25,4.45};...

  • Need help writing this code in python. This what I have so far. def display_menu(): print("======================================================================")...

    Need help writing this code in python. This what I have so far. def display_menu(): print("======================================================================") print(" Baseball Team Manager") print("MENU OPTIONS") print("1 - Calculate batting average") print("2 - Exit program") print("=====================================================================")    def convert_bat(): option = int(input("Menu option: ")) while option!=2: if option ==1: print("Calculate batting average...") num_at_bats = int(input("Enter official number of at bats: ")) num_hits = int(input("Enter number of hits: ")) average = num_hits/num_at_bats print("batting average: ", average) elif option !=1 and option !=2: print("Not a valid...

  • Python: def combo(): play = True while play: x = int(input('How many people\'s information would you...

    Python: def combo(): play = True while play: x = int(input('How many people\'s information would you like to see: ')) print()    for num in range(x): name() age() hobby() job() phone() print()       answer = input("Would you like to try again?(Enter Yes or No): ").lower()    while True: if answer == 'yes': play = True break elif answer == 'no': play = False break else: answer = input('Incorrect option. Type "YES" to try again or "NO" to leave": ').lower()...

  • this code is not working for me.. if enter 100 it is not showing the grades...

    this code is not working for me.. if enter 100 it is not showing the grades insted asking for score again.. #Python program that prompts user to enter the valuesof grddes . Then calculate the total scores , #then find average of total score. Then find the letter grade of the average score. Display the #results on python console. #grades.py def main(): #declare a list to store grade values grades=[] repeat=True #Set variables to zero total=0 counter=0 average=0 gradeLetter='' #Repeat...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT