简体   繁体   English

在 python 中遇到 sqrt() 问题,我正在使用 Visual Studio Code 软件进行 python 练习

[英]having sqrt() problem in python, I am using Visual Studio Code software to do the python practice

Why does my terminal show (8+0j) instead of 8.0 after input print(sqrt(64)) ?为什么我的终端在输入print(sqrt(64))后显示(8+0j)而不是8.0 Is it my setting fault?是我设置的错吗?

https://i.stack.imgur.com/Z6bCD.png https://i.stack.imgur.com/Z6bCD.png

I suppose you are using cmath ?我想你正在使用 cmath ? the sqrt function in cmath returns a complex number instead of a float You should import sqrt from math instead of cmath it return a float cmath 中的 sqrt 函数返回一个复数而不是一个浮点数 你应该从数学中导入 sqrt 而不是 cmath 它返回一个浮点数

from math import sqrt
print(sqrt(x))

I think this is true我认为这是真的

Try:尝试:

print(int(sqrt(49)))

pip install python-math <- install the math module if you haven't install it. pip install python-math <- 如果尚未安装数学模块,请安装它。

import the math module导入数学模块

import math导入数学

print(math.sqrt(4)) <- out put 2.0 print(math.sqrt(4)) <- 输出 2.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM