len(s) takes as an argument a
sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dicttionary, set, or frozen set).
That is, len(s)
can only be called - and only makes sense - on one of those data structures.
Passing an int
will fail with the posted error - it simply doesn't make sense, int
s don't have length.
TypeError: object of type 'int' has no len()
As such, something like len(range(1,100))
, or len([1,2,3])
works, but len(1)
doesn't.
In your example, in the comments:
for s in range(10):
if s>0:
print(len(s))
s
is an int, a different one in the range(10)
in every loop, which explains why len(s)
fails.
P.S. Posting a direct snippet from your code may help the answerers get a better understanding of the problem you are having.
Update: From your comments and code it is evident that you are trying to store a list
of inputs.
input_list = []
for s in range(0,6):
d=float(input())
if d>0:
input_list.append(d)
print("")
print (len(input_list))
When you do the assignment d=float(input())
, d
will be a different value every loop, a float
, hence len(d)
fails. What you want is to store all your inputs in a list input_list
and then print the len
of that. You can store values in a list by append()
ing them - input_list.append(d)