Home > database >  I have a double while loop, and it does not seem to be working because of some logic I'm doing
I have a double while loop, and it does not seem to be working because of some logic I'm doing

Time:07-15

I have a double while loop, and it does not seem to be working because of some logic I'm doing incorrectly. I'm not sure what is wrong exactly, but I feel the code may be too complicated and somewhere, there is an error.

enter code here
 import math
 print("How many numbers am I estimating John?")
 count = int(input("COUNT> "))
 print("Input each number to estimate.")
 better_guess = 0
 initial_guess = 10
 i = 0
 j = 0
 t = 1
 list = []
 for j in range(count):
   num = float(input("NUMBER> "))
   list.append(num)
   j = j   1
   if j == count:
     print("The square roots are as follows:")
 while i <= len(list):
   while t != 0 :
     initial_guess = 10
     better_guess = (initial_guess   (list[i])/initial_guess) / 2
     if initial_guess == better_guess:
      print(f"OUTPUT After {t} iterations, {list[i]}^0.5 = {better_guess}")
      i = i   1
      break
    initial_guess = better_guess
   i = i   1

CodePudding user response:

I suppose the following code works as you expect.

import math
print("How many numbers am I estimating John?")
count = int(input("COUNT> "))
print("Input each number to estimate.")
better_guess = 0
initial_guess = 10
i = 0
j = 0
t = 1
list = []
for j in range(count):
    num = float(input("NUMBER> "))
    list.append(num)
    j = j   1
    if j == count:
        print("The square roots are as follows:")
while i < len(list):
    initial_guess = 10
    while t != 0:
        better_guess = (initial_guess   (list[i])/initial_guess) / 2
        if initial_guess == better_guess:
            print(f"OUTPUT After {t} iterations, {list[i]}^0.5 = {better_guess}")
            t = t   1
            break
        initial_guess = better_guess
    i = i   1

enter image description here

You need to understand:

  1. We only need initialize guess once for each number. So do it in the first while loop;
  2. tneeds to be updated when initial_guess==better_guess rather than i, I believe this is a clerical error;
  3. initial_guessneeds to be updated in the second loop;

CodePudding user response:

There are some errors in your code, @x pie has pointed out some of them but not all. The most important is that you are need to initalize t for every number in the list, so you can get the iterations for the numbers separately. Also, t needs to be incremented in the while loop, not inside the if block.

You can also clean up the code considerably, for example the j variable is not being used, list comprehension can be used to shorten the code (pythonic way), and iterating over lists can be done with for num in list.

Putting this altogether produces this:

count = int(input('How many numbers am I estimating John? \nCOUNT> '))
print("Input each number to estimate.")
list = [float(input(f'NUMBER {i 1}> ')) for i in range(count)]
print("The square roots are as follows:")

for num in list:
    initial_guess = 10
    t = 0
    while True:
        better_guess = (initial_guess   num/initial_guess) / 2
        t  = 1
        if initial_guess == better_guess:
            print(f"OUTPUT After {t} iterations, {num}^0.5 = {better_guess}")
            break
        initial_guess = better_guess

Sample run:

How many numbers am I estimating John? 
COUNT> 4
Input each number to estimate.
NUMBER 1> 1
NUMBER 2> 9
NUMBER 3> 16
NUMBER 4> 4
The square roots are as follows:
OUTPUT After 9 iterations, 1.0^0.5 = 1.0
OUTPUT After 7 iterations, 9.0^0.5 = 3.0
OUTPUT After 7 iterations, 16.0^0.5 = 4.0
OUTPUT After 8 iterations, 4.0^0.5 = 2.0
  • Related