0
votes

i have a user input 'n' and i am finding the square root. Which i know is math.sqrt(n), but i need to make the program continue find the square root until it is less than 2. also return to the user how many times the program ran to find the root less than 2 using a counter. I am using python.

So far:

import math
root = 0
n = input('Enter a number greater than 2 for its root: ')

square_root = math.sqrt(n)
print 'The square root of', n, 'is', square_root

keep_going = 'y'

while keep_going == 'y':
    while math.sqrt(n) > 2:
        root = root + 1
4
If this is homework, please tag it as such. - Oddthinking
You should put your code into your original question. This will make it readable, especially given python's use of indentation. - JoshAdel

4 Answers

1
votes
import math
user_in = input()
num = int(user_in)

num = math.sqrt(num)
count = 1
while(num > 2):
  num = math.sqrt(num)
  count += 1

print count
0
votes

Assuming 2.x

count = 0
user_input = int(raw_input("enter:"))
while true:    
    num = math.sqrt( user_input )
    if num < 2: break
    print num
    count+=1
print count    

Error checking for user input not present.

0
votes

Obvious way:

def sqrtloop(n):
    x = 0
    while n >= 2:
        n **= 0.5
        x += 1
    return x

Not that much:

def sqrtloop2(n):
    if n < 2:
        return 0
    return math.floor(math.log(math.log(n, 2), 2)) + 1
0
votes
num = 0
while num < 100:
   
   num_sqrt = num ** 0.5
   print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))
   
   num = num + 1
print ("Good bye!")