-1
That is, when the variable number is randomly generated, it can be positive or negative:
I’m currently using:
if number < 0 :
number = number* -1
-1
That is, when the variable number is randomly generated, it can be positive or negative:
I’m currently using:
if number < 0 :
number = number* -1
1
Calculating the absolute value of the number with the function abs
:
numero = abs(numero)
By definition, the return of abs
will always be positive.
assert abs(1) == 1
assert abs(0) == 0
assert abs(-1) == 1
1
In the module Operator there is the function neg()
in which it allows us to apply an arithmetic negation, consisting of the inversion of the sign.
Import the function neg()
module operator
:
from operator import neg
Take the example:
num = -205
num = neg(num)
print(num)
Exit:
205
And the same works for positive values:
numP = 221
numP = neg(numP)
print(numP)
Exit:
-221
Vlw, it’s simpler than the way I was doing it. But what if a / numP always needed to be converted to positive when negative. But shouldn’t it be converted to negative when positive ? In this case, all in a positive / numP should be ignored ... Algma tip ?
@Vitoroliveira did not understand very well, you want to remove the signal only if it is negative? If yes, do a test neg(num) if num < 0 else num
.
Browser other questions tagged python-3.x
You are not signed in. Login or sign up in order to post.
The irony is that I’m Brazilian
– Vitor Oliveira