Reputation: 11
I am getting this error. Any idea on what to do to solve for my problem. The code produces good output however I am getting this error. Full disclosure this is a homework problem.
Traceback (most recent call last):
File "nt-test-c364d945", line 3, in <module>
assert(newton.newton(49) == 7.000000000000002)
AttributeError: module 'newton' has no attribute 'newton'
"""
File: newtons.py
Compute the square root of a number (uses function with loop).
1. The input is a number, or enter/return to halt the
input process.
2. The outputs are the program's estimate of the square root
using Newton's method of successive approximations, and
Python's own estimate using math.sqrt.
"""
import math
tolerance = 0.000001
def newton(x):
estimate = 1.0
while True:
estimate = ((estimate + x / estimate) / 2)
difference = abs(x - estimate ** 2)
if difference <= tolerance:
break
return estimate
def main():
while True:
x = input("Enter a positive number or enter/return to quit: ")
if x == '':
break
x = float(x)
print("The program's estimate is", newton(x))
print("Python's estimate is ", math.sqrt(x))
if __name__ == "__main__":
main()```
Upvotes: 1
Views: 191
Reputation:
Referring to your traceback, you're using newton.newton(49)
method in your script, but also you said that this newton funciton is inside newtons
module, so looks like you need to use newtons.newton(49)
(Don't forget to change import newton
to import newtons
)
Upvotes: 1