简体   繁体   English

我无法在 Visual Studio Code 上使用“from math import sqrt”

[英]I can't use "from math import sqrt" on Visual Studio Code

I'm new to this site, I'm recently trying to learn how to code in Python again, and now I'm doing an exercise that I could complete, but it's happening something that is annoying me.我是这个网站的新手,我最近正在尝试再次学习如何用 Python 编码,现在我正在做一个我可以完成的练习,但它发生了一些让我烦恼的事情。 I can import math successfully, but somehow, VSCode just doesn't want to recognize "from math import sqrt".我可以成功导入数学,但不知何故,VSCode 只是不想识别“从数学导入 sqrt”。 Here's my code so anyone can help me.这是我的代码,所以任何人都可以帮助我。 Thank you.谢谢你。

import math  # (from math import sqrt would be here, but doesn't work so I put import math)

a = int(input('Digite o primeiro número: '))
b = int(input('Digite o segundo número: '))

c = a**2+b**2
resultado = math.sqrt(c)

print(f'O comprimento da hipotenusa é de {resultado}')

If you do from math import sqrt then you have imported only sqrt and not math.如果您执行 from math import sqrt ,那么您只导入了 sqrt 而不是 math。 So in this case resultado = math.sqrt(c) should be resultado = sqrt(c) instead.所以在这种情况下resultado = math.sqrt(c)应该改为resultado = sqrt(c)

Mathias answered it in the comments; Mathias 在评论中回答了这个问题; thank you.谢谢你。

you dont need the math module, you can instead do:你不需要数学模块,你可以这样做:

c = (a**2+b**2)**0.5

as square rooting a number is equal to power it to 1/2.作为平方根,一个数字等于它的 1/2 次幂。

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

相关问题 我无法在 Visual Studio 代码中导入 openpyxl - I can't import openpyxl in visual studio code 我无法在 Visual Studio 代码中导入 pygame - i can't import pygame in visual studio code 即使我安装了它,也无法在 Visual Studio Code 中导入 Kivy - Can't import Kivy in Visual Studio Code even I installed it 无法在 Visual Studio Code 中导入库 - Can't Import Libraries in Visual Studio Code 我在我的 python 3.6 中安装了 tensorflow,但我无法将它导入我在 visual code studio 中的项目 - I installed tensorflow in my python 3.6 but i can't import it into my project in visual code studio 我如何在 Visual Studio 代码中导入其他模块 class - how can i import other module class in visual studio code “from math import sqrt”有效,但“import math”无效。 是什么原因? - "from math import sqrt" works but "import math" does not work. What is the reason? 在 python 中遇到 sqrt() 问题,我正在使用 Visual Studio Code 软件进行 python 练习 - having sqrt() problem in python, I am using Visual Studio Code software to do the python practice 如何在 Visual Studio Code 中使用 pip? - How can I use pip in Visual Studio Code? 为什么我不能从Visual Studio Code中选择所有已知的Python环境? - Why can't I choose all of my known Python Environments from Visual Studio Code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM