Skip to content

Listing 3.6 Squareroot.py prints interim values #4

@ChrisStokes

Description

@ChrisStokes

squareroot.py seems to read a list of eg, 20 interim values if the square root is at the average, ie if the high and low guesses are equidistant.
perhaps it should have a break after the first 'print (guess)' as in: def average(a,b):
return (a+b)/2

def squareRoot(num,low,high):
'''Finds the squareroot of num by
playing the Number guess/check strategy
by guessing over the range from "low" to high'''
for i in range(41):
guess = average(low,high)
if guess2 == num:
print(guess)
break # to return only one value for a whole number case.
elif guess
2 >num: #'Guess lower."
high = guess

    else: #'Guess higher.'
        low = guess

print(guess)

squareRoot(100, 2,75)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions