Python 计算用户输入数字的平方根

Python 计算用户输入数字的平方根,python,Python,编写一个程序,向用户请求浮点值,然后连续10次应用于它sqrt()。用两种不同的方法计算结果。(提示:sqrt()实际上是一个指数运算。) 这就是我得到的: from math import * def main(): n = eval(input("Please enter a whole number: ")) fact = 1 for i in range(10): n = sqrt(n)*fact print("In",i+1 , "Th

编写一个程序,向用户请求浮点值,然后连续10次应用于它sqrt()。用两种不同的方法计算结果。(提示:sqrt()实际上是一个指数运算。)

这就是我得到的:

from math import *

def main():
    n = eval(input("Please enter a whole number: "))
    fact = 1
    for i in range(10):
        n = sqrt(n)*fact
    print("In",i+1 , "The sqrt of :", n , "is", n)

main()
我想这样显示:例如,输入一个数字:16

在1中,16的sqrt为4

在2中,4的sqrt为2

。。。

在第10章中,sqrt为。。是


请帮助?

这是您更正的代码:

from math import *

def main():
    n = float(input("Please enter a whole number: "))
    for i in range(10):
        n = sqrt(n)
        print("In",i+1 , "The sqrt of :", n , "is", n)

main()
您需要将用户输入转换为浮点和
fact
变量。
eval
短语不正确

第二种方法是使用
**0.5

def other():
    n = float(input("Please enter a whole number: "))
    for i in range(10):
        n = n ** 0.5
        print("In", i+1 , "The sqrt of :", n , "is", n)

other()

虽然我不知道为什么您会在两方面需要它。

以下是您更正的代码:

from math import *

def main():
    n = float(input("Please enter a whole number: "))
    for i in range(10):
        n = sqrt(n)
        print("In",i+1 , "The sqrt of :", n , "is", n)

main()
您需要将用户输入转换为浮点和
fact
变量。
eval
短语不正确

第二种方法是使用
**0.5

def other():
    n = float(input("Please enter a whole number: "))
    for i in range(10):
        n = n ** 0.5
        print("In", i+1 , "The sqrt of :", n , "is", n)

other()
虽然我不知道为什么你会在两个方面需要它。

这里有另一个/优雅的方法来应用n次平方根:

>>> def comp_nth_sqrt(x, n):
...     return x**(0.5**n)
... 
>>> comp_nth_sqrt(4, 1)
2.0
>>> comp_nth_sqrt(4, 2)
1.4142135623730951
>>> comp_nth_sqrt(4, 10)
1.0013547198921082
如果您正在使用Python2,请使用

x = int(raw_input())
x = int(input())
如果您正在使用Python3,请使用

x = int(raw_input())
x = int(input())
去拿你的电话号码<代码>求值是不需要的,而且大多数情况下是邪恶的。

下面是另一种/优雅的方法来应用n次平方根:

>>> def comp_nth_sqrt(x, n):
...     return x**(0.5**n)
... 
>>> comp_nth_sqrt(4, 1)
2.0
>>> comp_nth_sqrt(4, 2)
1.4142135623730951
>>> comp_nth_sqrt(4, 10)
1.0013547198921082
如果您正在使用Python2,请使用

x = int(raw_input())
x = int(input())
如果您正在使用Python3,请使用

x = int(raw_input())
x = int(input())

去拿你的电话号码<代码>求值是不需要的,而且大多数时候都是邪恶的。

重提示
sqrt(x)==x^0.5
使用
int
(或者
float
,如果你想接受浮点值)而不是
eval
。你得到了更多吗?重提示
sqrt(x)==x^0.5
使用
int
(或
float
,如果您想接受浮点值)而不是
eval
。您有进一步的了解吗?您的意思是
**
,而不是
^
。您的意思是
**
,而不是
^