TypeError: object of type 'int' has no len() *subtraction*What's the canonical way to check for type in Python?Generating random numbers in Objective-CHow do I parse a string to a float or int?How to determine a Python variable's type?How to know if an object has an attribute in PythonShuffling a list of objectsWhat are the differences between type() and isinstance()?Determine the type of an object?How do I generate a random int number?Run a Python script from another Python script, passing in arguments

Would a 7805 5v regulator drain a 9v battery?

Converting 3x7 to a 1x7. Is it possible with only existing parts?

When is the phrase "j'ai bon" used?

Interview was just a one hour panel. Got an offer the next day; do I accept or is this a red flag?

Is swap gate equivalent to just exchanging the wire of the two qubits?

2 Managed Packages in 1 Dev Org

Does anyone recognize these rockets, and their location?

Why can't we feel the Earth's revolution?

What is the color associated with lukewarm?

Lead the way to this Literary Knight to its final “DESTINATION”

How did space travel spread through the galaxy?

Fill the maze with a wall-following Snake until it gets stuck

Is my research statement supposed to lead to papers in top journals?

High-end PC graphics circa 1990?

My student in one course asks for paid tutoring in another course. Appropriate?

Are there examples of rowers who also fought?

Why is Skinner so awkward in Hot Fuzz?

How to know whether to write accidentals as sharps or flats?

What is this plant I saw for sale at a Romanian farmer's market?

What are the mechanical differences between Adapt and Monstrosity?

Manager wants to hire me; HR does not. How to proceed?

Is there any effect in D&D 5e that cannot be undone?

Why can't I craft scaffolding in Minecraft 1.14?

Are there any super-powered aliens in the Marvel universe?



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


What's the canonical way to check for type in Python?Generating random numbers in Objective-CHow do I parse a string to a float or int?How to determine a Python variable's type?How to know if an object has an attribute in PythonShuffling a list of objectsWhat are the differences between type() and isinstance()?Determine the type of an object?How do I generate a random int number?Run a Python script from another Python script, passing in arguments






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I'm trying to figure out if there's a way to make a small calculation by subtracting a variables number from 4



number_1_raw = random.randrange(1, 1000)
number_1_len = len(number_1_raw)
number_of_zeros = 4 - number_1_len
print("0" * number_of_zeros + number_1_raw)


I wanted the script to give me an answer that ranged from 1-3, however the
code isn't executing properly.



The desired output would be something like this:
0617 or 0071 or 0008, etc.



Please help with this issue, thanks!










share|improve this question






























    1















    I'm trying to figure out if there's a way to make a small calculation by subtracting a variables number from 4



    number_1_raw = random.randrange(1, 1000)
    number_1_len = len(number_1_raw)
    number_of_zeros = 4 - number_1_len
    print("0" * number_of_zeros + number_1_raw)


    I wanted the script to give me an answer that ranged from 1-3, however the
    code isn't executing properly.



    The desired output would be something like this:
    0617 or 0071 or 0008, etc.



    Please help with this issue, thanks!










    share|improve this question


























      1












      1








      1








      I'm trying to figure out if there's a way to make a small calculation by subtracting a variables number from 4



      number_1_raw = random.randrange(1, 1000)
      number_1_len = len(number_1_raw)
      number_of_zeros = 4 - number_1_len
      print("0" * number_of_zeros + number_1_raw)


      I wanted the script to give me an answer that ranged from 1-3, however the
      code isn't executing properly.



      The desired output would be something like this:
      0617 or 0071 or 0008, etc.



      Please help with this issue, thanks!










      share|improve this question
















      I'm trying to figure out if there's a way to make a small calculation by subtracting a variables number from 4



      number_1_raw = random.randrange(1, 1000)
      number_1_len = len(number_1_raw)
      number_of_zeros = 4 - number_1_len
      print("0" * number_of_zeros + number_1_raw)


      I wanted the script to give me an answer that ranged from 1-3, however the
      code isn't executing properly.



      The desired output would be something like this:
      0617 or 0071 or 0008, etc.



      Please help with this issue, thanks!







      python python-3.x random






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 25 at 4:25









      AkshayNevrekar

      6,831102243




      6,831102243










      asked Mar 25 at 4:11









      Moses FordMoses Ford

      132




      132






















          4 Answers
          4






          active

          oldest

          votes


















          0














          You cant call len on an integer.




          The len() function returns the number of items in an object.
          When the object is a string, the len() function returns the number of
          characters in the string.




          so what you can do is




          number_1_len = len(str(number_1_raw))




          str changes int type to string type.






          share|improve this answer






























            0














            This line number_1_len = len(number_1_raw)  is problem. You are getting an integer value as number_1_len, and it has length of one. This number_1_len is actually the number of zeros that you want






            share|improve this answer






























              0














              Fist all I need to understands better what are happening...



              Fist



              number_1_raw = random.randrange(1, 1000)


              The random.randrange returns a number, probably int.



              Second



              number_1_len = len(number_1_raw)


              Has a problem, because number_1_raw is an int and len is a function that count the number of elements from an array, an object... but number_1_len isn't... that's an int and len can't count an int element...



              Is important you show all which error you're getting... and explain better what you're trying to do...






              share|improve this answer






























                0














                I think you need:



                import random

                number_1_raw = random.randrange(1, 1000)
                number_1_len = len(str(number_1_raw))
                number_of_zeros = 4 - number_1_len
                print(number_of_zeros)
                result = "0" * number_of_zeros

                print(result+str(number_1_raw))


                Output



                1 # number of zeros
                0801


                What's wrong with your code



                You are trying to find length of an integer number_1_raw which is giving you the error as you need iterables or string to find a length of that object. First convert it into string and then use len on it.



                Furthermore, in print statement you need to convert number_1_raw into string so that it can be appended. You can not perform + operation on str and int.






                share|improve this answer

























                  Your Answer






                  StackExchange.ifUsing("editor", function ()
                  StackExchange.using("externalEditor", function ()
                  StackExchange.using("snippets", function ()
                  StackExchange.snippets.init();
                  );
                  );
                  , "code-snippets");

                  StackExchange.ready(function()
                  var channelOptions =
                  tags: "".split(" "),
                  id: "1"
                  ;
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function()
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled)
                  StackExchange.using("snippets", function()
                  createEditor();
                  );

                  else
                  createEditor();

                  );

                  function createEditor()
                  StackExchange.prepareEditor(
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader:
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  ,
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  );



                  );













                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function ()
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55331164%2ftypeerror-object-of-type-int-has-no-len-subtraction%23new-answer', 'question_page');

                  );

                  Post as a guest















                  Required, but never shown

























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  0














                  You cant call len on an integer.




                  The len() function returns the number of items in an object.
                  When the object is a string, the len() function returns the number of
                  characters in the string.




                  so what you can do is




                  number_1_len = len(str(number_1_raw))




                  str changes int type to string type.






                  share|improve this answer



























                    0














                    You cant call len on an integer.




                    The len() function returns the number of items in an object.
                    When the object is a string, the len() function returns the number of
                    characters in the string.




                    so what you can do is




                    number_1_len = len(str(number_1_raw))




                    str changes int type to string type.






                    share|improve this answer

























                      0












                      0








                      0







                      You cant call len on an integer.




                      The len() function returns the number of items in an object.
                      When the object is a string, the len() function returns the number of
                      characters in the string.




                      so what you can do is




                      number_1_len = len(str(number_1_raw))




                      str changes int type to string type.






                      share|improve this answer













                      You cant call len on an integer.




                      The len() function returns the number of items in an object.
                      When the object is a string, the len() function returns the number of
                      characters in the string.




                      so what you can do is




                      number_1_len = len(str(number_1_raw))




                      str changes int type to string type.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Mar 25 at 4:16









                      Jibin MathewsJibin Mathews

                      629616




                      629616























                          0














                          This line number_1_len = len(number_1_raw)  is problem. You are getting an integer value as number_1_len, and it has length of one. This number_1_len is actually the number of zeros that you want






                          share|improve this answer



























                            0














                            This line number_1_len = len(number_1_raw)  is problem. You are getting an integer value as number_1_len, and it has length of one. This number_1_len is actually the number of zeros that you want






                            share|improve this answer

























                              0












                              0








                              0







                              This line number_1_len = len(number_1_raw)  is problem. You are getting an integer value as number_1_len, and it has length of one. This number_1_len is actually the number of zeros that you want






                              share|improve this answer













                              This line number_1_len = len(number_1_raw)  is problem. You are getting an integer value as number_1_len, and it has length of one. This number_1_len is actually the number of zeros that you want







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Mar 25 at 4:18









                              nhrcptnhrcpt

                              411624




                              411624





















                                  0














                                  Fist all I need to understands better what are happening...



                                  Fist



                                  number_1_raw = random.randrange(1, 1000)


                                  The random.randrange returns a number, probably int.



                                  Second



                                  number_1_len = len(number_1_raw)


                                  Has a problem, because number_1_raw is an int and len is a function that count the number of elements from an array, an object... but number_1_len isn't... that's an int and len can't count an int element...



                                  Is important you show all which error you're getting... and explain better what you're trying to do...






                                  share|improve this answer



























                                    0














                                    Fist all I need to understands better what are happening...



                                    Fist



                                    number_1_raw = random.randrange(1, 1000)


                                    The random.randrange returns a number, probably int.



                                    Second



                                    number_1_len = len(number_1_raw)


                                    Has a problem, because number_1_raw is an int and len is a function that count the number of elements from an array, an object... but number_1_len isn't... that's an int and len can't count an int element...



                                    Is important you show all which error you're getting... and explain better what you're trying to do...






                                    share|improve this answer

























                                      0












                                      0








                                      0







                                      Fist all I need to understands better what are happening...



                                      Fist



                                      number_1_raw = random.randrange(1, 1000)


                                      The random.randrange returns a number, probably int.



                                      Second



                                      number_1_len = len(number_1_raw)


                                      Has a problem, because number_1_raw is an int and len is a function that count the number of elements from an array, an object... but number_1_len isn't... that's an int and len can't count an int element...



                                      Is important you show all which error you're getting... and explain better what you're trying to do...






                                      share|improve this answer













                                      Fist all I need to understands better what are happening...



                                      Fist



                                      number_1_raw = random.randrange(1, 1000)


                                      The random.randrange returns a number, probably int.



                                      Second



                                      number_1_len = len(number_1_raw)


                                      Has a problem, because number_1_raw is an int and len is a function that count the number of elements from an array, an object... but number_1_len isn't... that's an int and len can't count an int element...



                                      Is important you show all which error you're getting... and explain better what you're trying to do...







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Mar 25 at 4:34









                                      GuilhermeGuilherme

                                      218




                                      218





















                                          0














                                          I think you need:



                                          import random

                                          number_1_raw = random.randrange(1, 1000)
                                          number_1_len = len(str(number_1_raw))
                                          number_of_zeros = 4 - number_1_len
                                          print(number_of_zeros)
                                          result = "0" * number_of_zeros

                                          print(result+str(number_1_raw))


                                          Output



                                          1 # number of zeros
                                          0801


                                          What's wrong with your code



                                          You are trying to find length of an integer number_1_raw which is giving you the error as you need iterables or string to find a length of that object. First convert it into string and then use len on it.



                                          Furthermore, in print statement you need to convert number_1_raw into string so that it can be appended. You can not perform + operation on str and int.






                                          share|improve this answer





























                                            0














                                            I think you need:



                                            import random

                                            number_1_raw = random.randrange(1, 1000)
                                            number_1_len = len(str(number_1_raw))
                                            number_of_zeros = 4 - number_1_len
                                            print(number_of_zeros)
                                            result = "0" * number_of_zeros

                                            print(result+str(number_1_raw))


                                            Output



                                            1 # number of zeros
                                            0801


                                            What's wrong with your code



                                            You are trying to find length of an integer number_1_raw which is giving you the error as you need iterables or string to find a length of that object. First convert it into string and then use len on it.



                                            Furthermore, in print statement you need to convert number_1_raw into string so that it can be appended. You can not perform + operation on str and int.






                                            share|improve this answer



























                                              0












                                              0








                                              0







                                              I think you need:



                                              import random

                                              number_1_raw = random.randrange(1, 1000)
                                              number_1_len = len(str(number_1_raw))
                                              number_of_zeros = 4 - number_1_len
                                              print(number_of_zeros)
                                              result = "0" * number_of_zeros

                                              print(result+str(number_1_raw))


                                              Output



                                              1 # number of zeros
                                              0801


                                              What's wrong with your code



                                              You are trying to find length of an integer number_1_raw which is giving you the error as you need iterables or string to find a length of that object. First convert it into string and then use len on it.



                                              Furthermore, in print statement you need to convert number_1_raw into string so that it can be appended. You can not perform + operation on str and int.






                                              share|improve this answer















                                              I think you need:



                                              import random

                                              number_1_raw = random.randrange(1, 1000)
                                              number_1_len = len(str(number_1_raw))
                                              number_of_zeros = 4 - number_1_len
                                              print(number_of_zeros)
                                              result = "0" * number_of_zeros

                                              print(result+str(number_1_raw))


                                              Output



                                              1 # number of zeros
                                              0801


                                              What's wrong with your code



                                              You are trying to find length of an integer number_1_raw which is giving you the error as you need iterables or string to find a length of that object. First convert it into string and then use len on it.



                                              Furthermore, in print statement you need to convert number_1_raw into string so that it can be appended. You can not perform + operation on str and int.







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Mar 25 at 4:37

























                                              answered Mar 25 at 4:23









                                              AkshayNevrekarAkshayNevrekar

                                              6,831102243




                                              6,831102243



























                                                  draft saved

                                                  draft discarded
















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid


                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.

                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55331164%2ftypeerror-object-of-type-int-has-no-len-subtraction%23new-answer', 'question_page');

                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                                  Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                                                  Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript