使用 python 进行平方根循环时遇到问题

发布于 2024-10-27 16:47:06 字数 398 浏览 3 评论 0原文

我有一个用户输入“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

海的爱人是光 2024-11-03 16:47:06
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
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
北凤男飞 2024-11-03 16:47:06

假设 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    

不存在对用户输入的错误检查。

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.

哽咽笑 2024-11-03 16:47:06

明显的方式:

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

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
好菇凉咱不稀罕他 2024-11-03 16:47:06
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!")
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!")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文