使用 python 进行平方根循环时遇到问题
我有一个用户输入“n”,我正在寻找平方根。我知道是 math.sqrt(n),但我需要让程序继续查找平方根,直到它小于 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设 2.x
不存在对用户输入的错误检查。
Assuming 2.x
Error checking for user input not present.
明显的方式:
没那么多:
Obvious way:
Not that much: