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

                                                  How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

                                                  155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

                                                  Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?