using the len() function











up vote
-3
down vote

favorite












i keep getting the error object type int has no len() not sure why. I just learned about the len function so an explanation of why its doing this would be great thanks



bits=int(input("enter an 8-bit binary number"))

for i in range (0,8):
if len(bits) >8 or len(bits) <8:
print("must enter an 8 bit number")









share|improve this question




















  • 1




    These are two separate problems. The first one is this, the second one is this.
    – timgeb
    Nov 10 at 22:32








  • 1




    if bits >8 and bits <8: will never be true because it's impossible for bits to be both > 8 and be < 8 (or any other value for that matter) at the same time.
    – martineau
    Nov 10 at 22:34

















up vote
-3
down vote

favorite












i keep getting the error object type int has no len() not sure why. I just learned about the len function so an explanation of why its doing this would be great thanks



bits=int(input("enter an 8-bit binary number"))

for i in range (0,8):
if len(bits) >8 or len(bits) <8:
print("must enter an 8 bit number")









share|improve this question




















  • 1




    These are two separate problems. The first one is this, the second one is this.
    – timgeb
    Nov 10 at 22:32








  • 1




    if bits >8 and bits <8: will never be true because it's impossible for bits to be both > 8 and be < 8 (or any other value for that matter) at the same time.
    – martineau
    Nov 10 at 22:34















up vote
-3
down vote

favorite









up vote
-3
down vote

favorite











i keep getting the error object type int has no len() not sure why. I just learned about the len function so an explanation of why its doing this would be great thanks



bits=int(input("enter an 8-bit binary number"))

for i in range (0,8):
if len(bits) >8 or len(bits) <8:
print("must enter an 8 bit number")









share|improve this question















i keep getting the error object type int has no len() not sure why. I just learned about the len function so an explanation of why its doing this would be great thanks



bits=int(input("enter an 8-bit binary number"))

for i in range (0,8):
if len(bits) >8 or len(bits) <8:
print("must enter an 8 bit number")






python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 0:49

























asked Nov 10 at 22:28









isaac fuller

263




263








  • 1




    These are two separate problems. The first one is this, the second one is this.
    – timgeb
    Nov 10 at 22:32








  • 1




    if bits >8 and bits <8: will never be true because it's impossible for bits to be both > 8 and be < 8 (or any other value for that matter) at the same time.
    – martineau
    Nov 10 at 22:34
















  • 1




    These are two separate problems. The first one is this, the second one is this.
    – timgeb
    Nov 10 at 22:32








  • 1




    if bits >8 and bits <8: will never be true because it's impossible for bits to be both > 8 and be < 8 (or any other value for that matter) at the same time.
    – martineau
    Nov 10 at 22:34










1




1




These are two separate problems. The first one is this, the second one is this.
– timgeb
Nov 10 at 22:32






These are two separate problems. The first one is this, the second one is this.
– timgeb
Nov 10 at 22:32






1




1




if bits >8 and bits <8: will never be true because it's impossible for bits to be both > 8 and be < 8 (or any other value for that matter) at the same time.
– martineau
Nov 10 at 22:34






if bits >8 and bits <8: will never be true because it's impossible for bits to be both > 8 and be < 8 (or any other value for that matter) at the same time.
– martineau
Nov 10 at 22:34














2 Answers
2






active

oldest

votes

















up vote
0
down vote













that should be easy, because python's "int" accepts "base" param which tells it
which numeric base to use



strbin = input('enter bin valuen')
converted = int(strbin,base=2)
print('base 2 converted to base 10 is: ', converted)





share|improve this answer




























    up vote
    0
    down vote













    You get that error because you read a string with input but immediately convert it to an int:



    bits=int(input("enter an 8-bit binary number"))
    --- there!


    An input such as "00110011" gets stored into bits as the decimal value 110011, without the leading zeroes. And as the error says, an int has no len.



    Remove the cast to int to make that part work. But there are lots of additional errors in your (original) code – I hope I got them all. (Pardon the exclamation marks, but all of your mistakes warranted at least one.)



    Your original code was



    bits=int(input("enter an 8-bit binary number"))

    for i in range (0,8):
    if bits >8 and bits <8:
    print("must enter an 8 bit number")

    if input >1:
    print("must enter a 1 or 0")
    else:
    rem=bits%10
    sum=((2**i)*rem)
    bits = int(bits/10)
    print(sum)


    adjusted to



    bits=input("enter an 8-bit binary number")

    sum = 0 # initialize variables first!

    if len(bits) != 8: # test before the loop!
    print("must enter an 8 bit number")
    else:
    for i in range (8): # default start is already '0'!
    # if i > 1: # not 'input'! also, i is not the input!
    if bits[i] < '0' or bits[i] > '1': # better also test for '0'
    print("must enter a 1 or 0")
    # else: only add when the input value is '1'
    elif bits[i] == '1':
    # rem = bits%10 # you are not dealing with a decimal value!
    # sum = ((2**i)*rem) # ADD the previous value!
    sum += 2**i
    # bits = int(bits/10) # again, this is for a decimal input

    # mind your indentation, this does NOT go inside the loop
    print(sum)





    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',
      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%2f53244051%2fusing-the-len-function%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      that should be easy, because python's "int" accepts "base" param which tells it
      which numeric base to use



      strbin = input('enter bin valuen')
      converted = int(strbin,base=2)
      print('base 2 converted to base 10 is: ', converted)





      share|improve this answer

























        up vote
        0
        down vote













        that should be easy, because python's "int" accepts "base" param which tells it
        which numeric base to use



        strbin = input('enter bin valuen')
        converted = int(strbin,base=2)
        print('base 2 converted to base 10 is: ', converted)





        share|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          that should be easy, because python's "int" accepts "base" param which tells it
          which numeric base to use



          strbin = input('enter bin valuen')
          converted = int(strbin,base=2)
          print('base 2 converted to base 10 is: ', converted)





          share|improve this answer












          that should be easy, because python's "int" accepts "base" param which tells it
          which numeric base to use



          strbin = input('enter bin valuen')
          converted = int(strbin,base=2)
          print('base 2 converted to base 10 is: ', converted)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 22:50









          nekomata

          1




          1
























              up vote
              0
              down vote













              You get that error because you read a string with input but immediately convert it to an int:



              bits=int(input("enter an 8-bit binary number"))
              --- there!


              An input such as "00110011" gets stored into bits as the decimal value 110011, without the leading zeroes. And as the error says, an int has no len.



              Remove the cast to int to make that part work. But there are lots of additional errors in your (original) code – I hope I got them all. (Pardon the exclamation marks, but all of your mistakes warranted at least one.)



              Your original code was



              bits=int(input("enter an 8-bit binary number"))

              for i in range (0,8):
              if bits >8 and bits <8:
              print("must enter an 8 bit number")

              if input >1:
              print("must enter a 1 or 0")
              else:
              rem=bits%10
              sum=((2**i)*rem)
              bits = int(bits/10)
              print(sum)


              adjusted to



              bits=input("enter an 8-bit binary number")

              sum = 0 # initialize variables first!

              if len(bits) != 8: # test before the loop!
              print("must enter an 8 bit number")
              else:
              for i in range (8): # default start is already '0'!
              # if i > 1: # not 'input'! also, i is not the input!
              if bits[i] < '0' or bits[i] > '1': # better also test for '0'
              print("must enter a 1 or 0")
              # else: only add when the input value is '1'
              elif bits[i] == '1':
              # rem = bits%10 # you are not dealing with a decimal value!
              # sum = ((2**i)*rem) # ADD the previous value!
              sum += 2**i
              # bits = int(bits/10) # again, this is for a decimal input

              # mind your indentation, this does NOT go inside the loop
              print(sum)





              share|improve this answer



























                up vote
                0
                down vote













                You get that error because you read a string with input but immediately convert it to an int:



                bits=int(input("enter an 8-bit binary number"))
                --- there!


                An input such as "00110011" gets stored into bits as the decimal value 110011, without the leading zeroes. And as the error says, an int has no len.



                Remove the cast to int to make that part work. But there are lots of additional errors in your (original) code – I hope I got them all. (Pardon the exclamation marks, but all of your mistakes warranted at least one.)



                Your original code was



                bits=int(input("enter an 8-bit binary number"))

                for i in range (0,8):
                if bits >8 and bits <8:
                print("must enter an 8 bit number")

                if input >1:
                print("must enter a 1 or 0")
                else:
                rem=bits%10
                sum=((2**i)*rem)
                bits = int(bits/10)
                print(sum)


                adjusted to



                bits=input("enter an 8-bit binary number")

                sum = 0 # initialize variables first!

                if len(bits) != 8: # test before the loop!
                print("must enter an 8 bit number")
                else:
                for i in range (8): # default start is already '0'!
                # if i > 1: # not 'input'! also, i is not the input!
                if bits[i] < '0' or bits[i] > '1': # better also test for '0'
                print("must enter a 1 or 0")
                # else: only add when the input value is '1'
                elif bits[i] == '1':
                # rem = bits%10 # you are not dealing with a decimal value!
                # sum = ((2**i)*rem) # ADD the previous value!
                sum += 2**i
                # bits = int(bits/10) # again, this is for a decimal input

                # mind your indentation, this does NOT go inside the loop
                print(sum)





                share|improve this answer

























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  You get that error because you read a string with input but immediately convert it to an int:



                  bits=int(input("enter an 8-bit binary number"))
                  --- there!


                  An input such as "00110011" gets stored into bits as the decimal value 110011, without the leading zeroes. And as the error says, an int has no len.



                  Remove the cast to int to make that part work. But there are lots of additional errors in your (original) code – I hope I got them all. (Pardon the exclamation marks, but all of your mistakes warranted at least one.)



                  Your original code was



                  bits=int(input("enter an 8-bit binary number"))

                  for i in range (0,8):
                  if bits >8 and bits <8:
                  print("must enter an 8 bit number")

                  if input >1:
                  print("must enter a 1 or 0")
                  else:
                  rem=bits%10
                  sum=((2**i)*rem)
                  bits = int(bits/10)
                  print(sum)


                  adjusted to



                  bits=input("enter an 8-bit binary number")

                  sum = 0 # initialize variables first!

                  if len(bits) != 8: # test before the loop!
                  print("must enter an 8 bit number")
                  else:
                  for i in range (8): # default start is already '0'!
                  # if i > 1: # not 'input'! also, i is not the input!
                  if bits[i] < '0' or bits[i] > '1': # better also test for '0'
                  print("must enter a 1 or 0")
                  # else: only add when the input value is '1'
                  elif bits[i] == '1':
                  # rem = bits%10 # you are not dealing with a decimal value!
                  # sum = ((2**i)*rem) # ADD the previous value!
                  sum += 2**i
                  # bits = int(bits/10) # again, this is for a decimal input

                  # mind your indentation, this does NOT go inside the loop
                  print(sum)





                  share|improve this answer














                  You get that error because you read a string with input but immediately convert it to an int:



                  bits=int(input("enter an 8-bit binary number"))
                  --- there!


                  An input such as "00110011" gets stored into bits as the decimal value 110011, without the leading zeroes. And as the error says, an int has no len.



                  Remove the cast to int to make that part work. But there are lots of additional errors in your (original) code – I hope I got them all. (Pardon the exclamation marks, but all of your mistakes warranted at least one.)



                  Your original code was



                  bits=int(input("enter an 8-bit binary number"))

                  for i in range (0,8):
                  if bits >8 and bits <8:
                  print("must enter an 8 bit number")

                  if input >1:
                  print("must enter a 1 or 0")
                  else:
                  rem=bits%10
                  sum=((2**i)*rem)
                  bits = int(bits/10)
                  print(sum)


                  adjusted to



                  bits=input("enter an 8-bit binary number")

                  sum = 0 # initialize variables first!

                  if len(bits) != 8: # test before the loop!
                  print("must enter an 8 bit number")
                  else:
                  for i in range (8): # default start is already '0'!
                  # if i > 1: # not 'input'! also, i is not the input!
                  if bits[i] < '0' or bits[i] > '1': # better also test for '0'
                  print("must enter a 1 or 0")
                  # else: only add when the input value is '1'
                  elif bits[i] == '1':
                  # rem = bits%10 # you are not dealing with a decimal value!
                  # sum = ((2**i)*rem) # ADD the previous value!
                  sum += 2**i
                  # bits = int(bits/10) # again, this is for a decimal input

                  # mind your indentation, this does NOT go inside the loop
                  print(sum)






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 11 at 12:58

























                  answered Nov 11 at 12:48









                  usr2564301

                  17.2k73270




                  17.2k73270






























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f53244051%2fusing-the-len-function%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

                      Florida Star v. B. J. F.

                      Danny Elfman

                      Lugert, Oklahoma