-5

When I need to count the number of positive numbers in a given range (ex, range-->>(1,100)), I use function len(), but it doesn't work and gives:

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

Can someone explain this error, or suggest the correct function to use with this problem.

Maha Taha
  • 7
  • 1
  • 7
  • 4
    `len` is the right function to use to get the length of a sequence, but it sounds like you're not calling it on a sequence but rather a single integer. Please show the code you're running and the full exception traceback and we'll be able to help you understand what's happening. – Blckknght Aug 10 '16 at 02:04
  • `len(1)` produces the given `TypeError` exception; `len(range(1,100))` returns 99, as expected. As [Blckknght](http://stackoverflow.com/users/1405065/blckknght) says, we need to see your problem code. – AJNeufeld Aug 10 '16 at 02:17
  • @MahaTaha: What are you trying to do there? You're generating `int`s, which don't have a concept of length, why would you think `len` would work? – ShadowRanger Aug 10 '16 at 02:25
  • @MahaTaha: Please edit the code into the question so you can format it properly. – Blckknght Aug 10 '16 at 02:47
  • @MahaTaha: Again, what on Earth are you expecting? In the new code, you're trying to take the length of what you just made a `float`. What is the length of a `float` to you? Are you trying to accumulate several `float`s in a `list`? If you can't figure that out, stop trying to write this code and use a proper Python tutorial instead of flailing wildly and hoping. Or take a class that provides hands-on assistance, this sort of ultra-basic stuff isn't practical to teach on StackOverflow question at a time. – ShadowRanger Aug 10 '16 at 03:00
  • my input six nos EX( 7 , -5 , 6 , -3.4 , 4.6 , 12 )... and the output tell me that there are (4 positive no)....Understand me ??!!!. – Maha Taha Aug 10 '16 at 03:04
  • It's really a bad idea to code something before you've read at least Python Tutorial https://docs.python.org/3/tutorial/ – thodnev Aug 10 '16 at 07:15

3 Answers3

2

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, ints 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)

Ramon
  • 1,169
  • 11
  • 25
  • thanks for your effort man ... it's work – Maha Taha Aug 10 '16 at 03:21
  • @MahaTaha My pleasure! Always aim to post a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) in your questions, so that SO users can better help you. I'd recommend that you read a good [Python Tutorial](https://docs.python.org/3.5/tutorial/) to get a good grasp of these things, and read more about python's [Data Structures](https://docs.python.org/3.5/tutorial/datastructures.html). As well, remember to upvote any good questions, and select an answer (if there is one that answers your question), after you have given everyone enough time to answer (a day or so). – Ramon Aug 10 '16 at 03:32
  • i got it ... thanks so much for UR advice .... but i've Q: how i can get the ave of this positive nos – Maha Taha Aug 10 '16 at 03:35
  • [sum(s)](https://docs.python.org/3/library/functions.html#sum)/[len(s)](https://docs.python.org/3.5/library/functions.html#len) `sum(input_list)/len(input_list)` for your code. Trying googling such question, [this](http://stackoverflow.com/questions/9039961/finding-the-average-of-a-list) shows as the top result, and it has the answer. – Ramon Aug 10 '16 at 03:41
0

You are likely calling len on an individual integer rather than the output of the range. Instead, use it as such:

>>> len(range(1, 100))
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
0

The easy way to solve this problem is to filter the range and then count the results:

num_positives = len([i for i in range(1, 10) if i > 0])
print(num_positives)
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284