没有math.sqrt的数字的平方根

时间:2017-07-17 02:42:06

标签: python python-3.x math

export class VideosService {
result;
constructor(private http: Http, public router: Router) { }

getVideos() {
    this.http.get('http://localhost:3000/videos?sessionId=' + localStorage.getItem('sessionId'))
        .map(response => response.json())
        .subscribe(result => this.result = result);
        localStorage.setItem('videos', this.result);
        console.log(localStorage);
}

这给了我一个名称错误,“NameError:name'root'未定义”

我想在没有math.sqrt或import subprocess word = input("select word to filter: ") subprocess.call(['/bin/grep', "-w", word, "textFile.txt"]) #str is not needed 的情况下这样做 如何在不使用这两个的情况下解决这个问题,为什么不会出现相同的错误,因为如果我使用替代方法,“NameError:name'number'未定义”?

1 个答案:

答案 0 :(得分:4)

Python不是像Wolfram Alpha这样的方程求解器。它是一种执行您提供的指令的语言。

root = number**(1/2)

是一条指令。它说:取{已存在的number,并将其提升到1/2的力量;将结果命名为root

number = root**2

也是一个指令,但不是你想要的指令。它说:取root,它应该已经存在,然后把它提升到第二种力量;将结果命名为number。但root实际上并不存在,因此您会收到错误。

您提供的两个选项是找到平方根最明智的选项 - 不确定为什么要避免它们。当然,您可以自己实现平方根查找算法,但使用内置解决方案肯定要简单得多!一个小的改动可能是使用number**(.5)math.pow(number,1/2)(当然,这相当于你的1/2)。

相关问题