id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

ferrer@hendrix.edu > find_square_root
prev  |  next  |  chance

Write a function to find the square root of a number. Write a while loop that, starting with 2, tests possible square roots in ascending order by squaring the value and comparing it to n. Return the value that, when squared, is as close as possible to n without going over it. For example, to find the square root of 16, the function will first find 2**2, which is 4. Next, it will find 3**2, which is 9. Finally, it will find 4**2, which is 16, so it will return 4. Finding the square root of 15 is similar but not identical. The function first finds 2**2, which is 4, so it next tries 3**2. That is 9, so it tries 4**2. Since 16 is greater than 15, it stops. But since it is greater than the target value, it returns 3 instead of 4.


find_square_root(4) → 2
find_square_root(9) → 3
find_square_root(7) → 2

...Save, Compile, Run (ctrl-enter)

def find_square_root(n):

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Python Help

Post-solution available

Copyright Nick Parlante 2017 - privacy