【发布时间】:2011-03-29 02:41:10
【问题描述】:
我有一个用户输入“n”,我正在寻找平方根。我知道是 math.sqrt(n),但我需要让程序继续找到平方根,直到它小于 2。还返回给用户程序运行了多少次以使用 a 找到小于 2 的根柜台。我正在使用 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
【问题讨论】:
-
如果这是作业,请标记它。
-
您应该将代码放入原始问题中。这将使它具有可读性,尤其是考虑到 python 使用缩进。
标签: python loops square-root