Problems with import and from/import command

Asked

Viewed 66 times

-1

I have a problem with my show. When I type "from Math import sqrt (or anything else", the Math library is not recognized by pycharm. Follows the code:

from math import sqrt
n = int(input('Digite um número: '))
r = math.sqrt(n)
print('A raiz de {} é {}'.format(n, math.ceil(r)))

Maybe it’s a very simple problem. But I still can’t solve it.

2 answers

0

Hello, welcome(a) to the website!

In your code, you did not import the entire Math library, only Math.sqrt(). Soon, Math.Ceil() will not be available. To solve:

from math import sqrt, ceil

or

import math 

The first option is best by weighing less the program. The second matter thing you will not use.

Hug!

0

You’re not importing the module math, but only the function sqrt directly.

To solve this you just need to change the line import of this:

from math import sqrt

For this:

import math

Now, if you want to use the functions without always adding the prefix math., you can import the functions sqrt and ceil in this way:

from math import sqrt, ceil

And in the corresponding lines, you would call the functions this way:

r = sqrt(n)
print('A raiz de {} é {}'.format(n, ceil(r)))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.