-1

for any negative number, it also counts the "-" as a character even though its a part of that number. I tried to make my numbers string first but it won't change the outcome, it always gives +1 to the actual amount of characters involved.

code :

def cube(num):
    return str(num*num*num)
kappa=(cube(-5))
if len(kappa)<4:
    print(kappa)
elif len(kappa)>=4 and len(kappa)>=0:
    print(kappa + "   ,your number is bigger than 999")

outcome :

-125 ,your number is bigger than 999

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
HARD YOLO
  • 11
  • 1

4 Answers4

1

len is checking the length of the string "-2", which has two characters.

TheDude
  • 3,796
  • 2
  • 28
  • 51
1

Because you're taking the length of a string, the minus sign is preserved; that's the logical thing to do, why would we want to remove it? If you're interested in the number of digits, just remove the minus sign before counting the number of chars:

n = '-125'
len(str(abs(int(n))))
=> 3
Óscar López
  • 232,561
  • 37
  • 312
  • 386
1

Once you put quotes around an integer, say -2, it's no longer an integer. It becomes a string literal, so - and 2 are considered as characters, just like 'a', 'b', etc.

To get the absolute integer value, do:

abs(int(kappa))
entropy
  • 840
  • 6
  • 16
0

I don't know what do you mean. But here is simple comparison program.

def cube(num):
    return str(num*num*num)

kappa=int(cube(-5))

if kappa<4:
    print(kappa)
elif kappa>=4:
    print(kappa + " ,your number is bigger than 4")
Hasee Amarathunga
  • 1,901
  • 12
  • 17