5 Replies - 1435 Views - Last Post: 13 October 2014 - 12:35 PM Rate Topic: -----

#1 Aye DH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-October 14

TypeError: object of type 'int' has no len()

Posted 13 October 2014 - 09:49 AM

Hello. Well, after being trying for hours to fix this error I've come to the conclusion that I can't fiz it. I can't understand why I get this error. I'm trying to check if the input value from the user has 8 numbers, and if not, give him an error messatge. Here is the code:
def main():
    letters = 'TRWAGMYFPDXBNJZSQVHLCKE'

    dni = int(raw_input('Give me the number of your dni:'))
    
    rest = dni % 23
    if len(dni) == 8:
        print 'The letter of your dni is: ' , (letters[rest])
    
    else:
        Print ("The value is not correct!")
main()


And the error is "object of type 'int' has no len()"

I'm trying to assign a letter depending on numbers given (must be 8 numbers)
It's for homework. Obioubsly i don't want you to do my homework but if you could only help me a bit would be perfect. Thanks in advance!

This post has been edited by modi123_1: 13 October 2014 - 10:04 AM
Reason for edit:: fixed botched code tag


Is This A Good Question/Topic? 0
  • +

Replies To: TypeError: object of type 'int' has no len()

#2 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6409
  • View blog
  • Posts: 25,903
  • Joined: 12-December 12

Re: TypeError: object of type 'int' has no len()

Posted 13 October 2014 - 11:05 AM

You have to convert dni back to a string temporarily:
    if len(str(dni)) == 8:

I would treat the input as a string firstly, then test if it is a number.

This post has been edited by andrewsw: 13 October 2014 - 11:09 AM

Was This Post Helpful? 1
  • +
  • -

#3 Aye DH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-October 14

Re: TypeError: object of type 'int' has no len()

Posted 13 October 2014 - 12:01 PM

Firstly, thanks for the answer! I tried to convert dni to string but it gives me the error "TypeError: not all arguments converted during string formatting". I don't know what it means. I searched a little about the problem but i didn't found anything. I'll keep working to fix it but if you know what it means please tell it to me =D. Thanks!

This post has been edited by andrewsw: 13 October 2014 - 12:05 PM
Reason for edit:: removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6409
  • View blog
  • Posts: 25,903
  • Joined: 12-December 12

Re: TypeError: object of type 'int' has no len()

Posted 13 October 2014 - 12:05 PM

You'll need to post your attempt as I provided the modified line for you.
Was This Post Helpful? 1
  • +
  • -

#5 Aye DH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-October 14

Re: TypeError: object of type 'int' has no len()

Posted 13 October 2014 - 12:16 PM

View Postandrewsw, on 13 October 2014 - 12:05 PM, said:

You'll need to post your attempt as I provided the modified line for you.


Sorry! I didn't put the str right. Now it works! Thank you a lot for helping me! Really, thank you mate. You're the best! =D.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6409
  • View blog
  • Posts: 25,903
  • Joined: 12-December 12

Re: TypeError: object of type 'int' has no len()

Posted 13 October 2014 - 12:35 PM

You're welcome ;)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1