我有一个用户输入'n',我找到了平方根。我所知道的是math.sqrt(n),但是我需要让程序继续找到平方根,直到它小于2.还要返回给用户程序运行多少次以找到小于2的根使用a计数器。我正在使用python。
到目前为止:
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
答案 0 :(得分:1)
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
答案 1 :(得分:0)
假设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
检查用户输入不存在时出错。
答案 2 :(得分:0)
明显的方式:
def sqrtloop(n):
x = 0
while n >= 2:
n **= 0.5
x += 1
return x
没那么多:
def sqrtloop2(n):
if n < 2:
return 0
return math.floor(math.log(math.log(n, 2), 2)) + 1
答案 3 :(得分:0)
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!")