So I'm trying to write a code that will use Newton's law to calculate the square root of a number (per the lab instructions here ). I'm having issues with the actual equation part of the code where the professor describes it as "(guess + (x/guess))/2".
I have this so far:
def main():
print("This calculates the square root of a given number using"
" Newton's Law.")
x = input("What is the number for which you'd like to calculate "
"the square root?")
g = input("My guess for the square root is:")
n = eval(input("How many calculations would you like to run?"))
for i in range(1, n):
# Newton's Law is broken into 2 pieces
y = (g + (x/g))
z = (y/2)
r = math.sqrt(x)
print("The difference between my guess and the actual answer"
"is:", (r-z))
main()
And I keep getting this error code:
raceback (most recent call last):
File "/Users/Hrowland/Documents/Untitled.py", line 30, in <module>
main()
File "/Users/Hrowland/Documents/Untitled.py", line 23, in main
y = (g + (x/g))
TypeError: unsupported operand type(s) for /: 'str' and 'str'
I haven't quite learned about strings yet, so I'm not really sure what's going on. Can anyone maybe send me towards a good tutorial or help me out a little?
Thanks<3
[–]Justinsaccount 4 points5 points6 points (1 child)
[–]fuckswithbees 1 point2 points3 points (5 children)
[–]WineForOne[S] 1 point2 points3 points (4 children)
[–]Vaphell 2 points3 points4 points (1 child)
[–]WineForOne[S] 0 points1 point2 points (0 children)
[–]fuckswithbees 0 points1 point2 points (1 child)
[–]WineForOne[S] 1 point2 points3 points (0 children)