• Register
1 vote
156 views

Problem:

I am doing a course on python programming language. As a begginer I have to do a lot of programming practice and some of them are very confusing. Today I wrote a code to do some counting problems in python. The code I wrote is:

def count(serial, totals):
    found = 0
    for i in len(serial):
        if serial[i] == totals:
           found = found + 1
    return found

But when I run or compile this program it throws error and shows me an error message:

TypeError: 'int' object is not iterable

What is the problem in my programm and what should I do now. Can anyone explain me this error. Thanks in advance.

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

1 vote

Answer:

The error you are getting because 

"TypeError: 'int' object is not iterable" is caused by the statement for num in 4 because the for statement operates on iterable data such as a string, list, tuple, or other object container

What to do:

You need to use i to iterate through a list, but you specified an integer, named len(sequence), instead of the necessary list

So write your program like below:

def count(serial, totals):
    found = 0
    for i in range(len(serial)):
        if serial[i] == totals:
           found = found + 1
    return found

Hope you understand this .

If you still do not understand fell free to ask me any question.

11 6 4
34,950 points

Related questions

0 votes
1 answer 19 views
19 views
Problem: I am looking to make a loop so that for each element in the list, it will get broken down into each of it's characters. For example, say the number 137 was in the list then it would be turned into [1,3,7]. Then I want to add these numbers together (I ... it). However, I keep getting the error message TypeError: 'int' object is not iterable when I try and run this. What am I doing wrong?
asked Feb 23 Mashhoodch 13k points
0 votes
1 answer 24 views
24 views
Problem: I’m working on the count problem, and have come up with the following code: def count(sequence, item): found = 0 for i in len(sequence): if sequence[i] == item: found = found + 1 return found
asked Jan 22 Mashhoodch 13k points
0 votes
1 answer 11 views
11 views
Problem: How to I Solve this: python typeerror: 'nonetype' object is not iterable HELP! I need help understanding this: python typeerror: 'nonetype' object is not iterable Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 3 Mashhoodch 13k points
0 votes
1 answer 3 views
3 views
problem: I need someone to understand the problem and solve the issue of typeerror: 'int' object is not iterable ?
asked Apr 5 ashik 14.6k points
1 vote
2 answers 149 views
149 views
Problem: Well, I am getting an error from my python program that says, argument of type 'int' is not iterable. Could anybody save my day by fixing the error? Find my sample code snippet below def duplicates(n): my_list = n[0] for i in range(1,len(n)): if n[i] in my_list: print "duplicate " + str(n[i]) else: out.append(n[i]) return my_list duplicates([4,5,5,4]) Thanks.
asked Apr 4, 2020 Gavin 15.3k points
0 votes
1 answer 64 views
64 views
Problem: What the traceback error is pointing out is the misuse of for statement: for i in Updt():. for in python 3 i "Python's for statement iterates? How function' object is not iterable. from getWords import getWords unique = [] def findUniqueWords(words): for word in words
asked Feb 15 Mashhoodch 13k points
1 vote
1 answer 498 views
498 views
Problem: When I run or compile this program import urllib2 import time import datetime HolaAndre = 'HUA','WALT','NOKA','CMG','EVAL','DARZ','SYM' def BackData(stock): try: print 'Still pulling',stock print str(datetime.datetime.fromtimestamp(time. ... BackData(eachStock) It shows me the following error main loop 'builtin_function_or_method' object is not iterable What is the problem please help me.
asked Apr 5, 2020 LizzyM 6.1k points
1 vote
1 answer 50 views
50 views
Problem: I am begginer in python programming language and I am doing a course in it. I was doing some code to practice python programing. I wrote this code while doing my practice from __future__ import division x = 66 y = 99 z = 33 round((x/y)*0.8*z) But it shows an ... not callable Is there any problem in my code. I am using python 2.7 as my default python version . I am new so please help me.
asked Apr 4, 2020 LizzyM 6.1k points
0 votes
1 answer 24 views
24 views
Problem: how to I Solve this: typeerror: a bytes-like object is required, not 'int' HELP! I need help understanding this: typeerror: a bytes-like object is required, not 'int' Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 8 Mashhoodch 13k points
0 votes
1 answer 14 views
14 views
Problem: If someone wants to itterate through all objects in an array of URL objects but I got. Can not unpack non-iterable float object.
asked Mar 22 PkGuy 14k points