0

I'm getting seemingly unfounded errors with this seemingly easy program..

the variables "guess" and "i" are user input

```
for guess in range (i):
float(guess + float(x / guess)) / 2
print guess
```

I'm getting: ZeroDivisionError: integer division or modulo by zero

This Question has been **Answered**

0

That did help, but I must have something really screwed up with the actual implementation of the for statement. Here is the full program.

```
import math
from random import randint
x = randint(2, 100)
print "The root is", x
def main():
guess = input("Guess what the square root is: ")
i = input("How many times should Newton's improve the guess?: ")
print "~" * 50
for guess in range (1, i):
float((float(x / guess) + guess) / 2)
print "Your final guess was", guess, "."
answer = float(math.sqrt(x))
check = abs(float(guess - answer))
print "The right answer was", answer, "..."
if check == 0:
print "Your guess was right on!"
else :
print "You were", check, "away from the correct answer"
main()
```

I get this as an output:

```
The root is 90
Guess what the square root is: 9
How many times should Newton's improve the guess?: 15
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Your final guess was 14 .
The right answer was 9.48683298051 ...
You were 4.51316701949 away from the correct answer
```

So, apparently the for statement makes my guesses worse. Thanks in advance

0

So, apparently the for statement makes my guesses worse. Thanks in advance

Look at what `range(15)`

and `range(1,15)`

produce:

```
>>> range(15)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> range(1,15)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>>
```

You're leaving off the number 15 (if that's what you were shooting for).

Now just out of curiosity, what is the purpose behind your for loop? As it stands, it isn't doing anything. It's simply doing some arbitrary calculations but never updating any values or storing any results.

Also, what's with the multiple float casting? Only one operand needs to be a float in order to invoke true division in Python < 3.0 ... that means you could simply put `guess = float(guess)`

before the calculation line and remove all float casting and achieve the same output. **EDIT** Actually, I just checked and it's actually waaay more accurate to remove that float casting and simply use `guess = float(guess)`

followed by `result = ((x / guess) + guess) / 2`

.

0

Actually the more I try to get this code working the more I realize that you might be confusing your variable names.... you're iterating over guess in your for loop, but then also using it afterwards to say what the final guess was... which will always be the last value of the for loops iteration. So on a `range(1,15)`

call, guess will start at `guess = 1`

, then on the next iteration it'll be `guess = 2`

, then `guess = 3`

, and so on until it becomes `guess = 14`

to end the loop.

Also, you never convert the "number of times" that the user enters to a number, you're leaving it as a string... try to wrap that input with a call to `int()`

I also should re-state that you need to re-think the way you're casting your floats... instead of the following:

```
for guess in range (1, i):
float((float(x / guess) + guess) / 2)
answer = float(math.sqrt(x))
check = abs(float(guess - answer))
```

You should have these lines:

```
for some_value in range (1, i):
guess = ((x / guess) + guess) / 2
answer = math.sqrt(x)
check = abs(guess - answer)
```

And one last thing: You should use raw_input instead of input (if this is a 2.X version of Python). Check what happens if the user enters `4 * 5`

into an `input()`

prompt and then think about what would happen if they entered any malicious code....

This question has already been answered. Start a new discussion instead.

Recommended Topics

Hi there,

I want to implement undo logic which deletes the last drawn shape. To test this logic i had cleared shapes list and redrawn all shapes but still **last** ...

Hello eveyone,

Please tell me why triangle shape is redrawing on mouse move. It want triangle shape draw only once until mouse release.

Here is my code below. Thank you ...