6

Here's my code:

TestV = int(input("Data: "))
print TestV
print len (TestV)

In fact, it prints TestV but when I try to get its length it gives me the error "len() of unsized object"

I've try something easier like

>>> x = "hola"
>>> print len(x)
4

and it works perfectly. Can anyone explain to me what am I doing wrong?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Giancarlo Benítez
  • 428
  • 1
  • 4
  • 19

2 Answers2

6

If you want to count the number of digits, you need to convert it back to a String (note that the minus sign (-) will count as one as well):

len(str(TestV))

Or simply wait with converting it to an int:

TestV = input("Data: ")
print len (TestV)
TestV = int(TestV)

Note however than in the latter case, it will count tailing spaces, etc. as well.

EDIT: based on your comment.

The reason is that strings containing other characters are likely not to be numbers, but an error in the process. In case you want to filter out the number, you can use a regex:

import re

x = input("Data: ")
x = re.sub("\D", "", x)
xv = int(x)
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • thanks! that works. But now I have another question. If its supposed that i'm working with Strings why it doesn't recognize numbers and characters mixed? like "ac4ft6" – Giancarlo Benítez Feb 18 '15 at 17:40
3

Type int does not have length.

You are basically turning any input to a type int, in
TestV = int(input("Data: "))

taesu
  • 4,482
  • 4
  • 23
  • 41
  • thanks for the explanation ^^ But now I have another question. If its supposed that i'm working with Strings why it doesn't recognize numbers and characters mixed? like "ac4ft6" – Giancarlo Benítez Feb 18 '15 at 17:56