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")
python
add a comment |
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")
python
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 forbits
to be both > 8and
be < 8 (or any other value for that matter) at the same time.
– martineau
Nov 10 at 22:34
add a comment |
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")
python
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
python
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 forbits
to be both > 8and
be < 8 (or any other value for that matter) at the same time.
– martineau
Nov 10 at 22:34
add a comment |
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 forbits
to be both > 8and
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
add a comment |
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)
add a comment |
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)
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
answered Nov 10 at 22:50
nekomata
1
1
add a comment |
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
edited Nov 11 at 12:58
answered Nov 11 at 12:48
usr2564301
17.2k73270
17.2k73270
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 forbits
to be both > 8and
be < 8 (or any other value for that matter) at the same time.– martineau
Nov 10 at 22:34