Hi I'm getting the error TypeError: argument of type 'int' is not iterable when i run my program and don't know where I'm going wrong.

My code is:

class loan:

    global loanlist
    loanlist = []

    def loanbook(self, name, ISBN, author, title):
	self.name = name
	self.ISBN = ISBN
	self.author = author
	self.title = title
	self.data = loanlist.append(self.name)
	self.data = loanlist.append(self.ISBN)
	self.data = loanlist.append(self.author)
	self.data = loanlist.append(self.title)

class book:

    global booklist
    booklist = []

    def addbook(self, ISBN, author, title, stock):
	self.ISBN = ISBN
	self.author = author
	self.title = title
	self.stock = stock
	self.data = booklist.append(self.ISBN)
	self.data = booklist.append(self.author)
	self.data = booklist.append(self.title)
	self.data = booklist.append(self.stock)

class library:

    def issamebook(self, name, ISBN, author, title):
	for i in range(len(booklist)):
	    if ISBN in booklist[i]:
	        if author in booklist[i+1]:
		    if title in booklist[i+2]:
		        print "Found book match at position ", i
			booklist[i+3] = booklist[i+3] - 1
		    else:
		        print "book not found"
	        else:
		    pass
	    else:
	        pass

if __name__ == "__main__":
    a=book()
    a.addbook("1234", "John", "Book 1", 1)
    a.addbook("2345", "Jim", "Book 2", 1)
    print booklist
    b=loan()
    b.loanbook("Steven", "1234", "John", "Book 1")
    b.loanbook("Tom", "2345", "Jim", "Book 2")
    print loanlist
    c=library()
    c.issamebook("Steven", "1234", "John", "Book 1")
    print booklist

Can someone give me a clue on what to do. Thanks.

Dani AI

Generated

You are hitting the membership operator on an integer. In your flat booklist, every 4th item is the stock count (an int). When the loop lands on that position, if ISBN in booklist[i]: becomes a membership test against an int, which raises TypeError: argument of type 'int' is not iterable. As @Ene Uran noted, iterating in strides of 4 avoids the exception, but it is still brittle because any indexing slip will silently compare the wrong fields. The in operator only works on containers (or objects that implement __contains__/iteration). Using it on non-iterables like int triggers a TypeError; see Python’s language reference on membership tests for the formal rules (Membership test operations).

Two additional cleanups:

  • Do not assign the result of append. It mutates the list and returns None, so self.data = booklist.append(...) just stores None. This is by design for mutating methods in Python’s built-ins (Built-in types: mutating methods return None).
  • Avoid mixing tabs and spaces; stick to spaces for consistent indentation.

A more robust approach is to model books as records and index by ISBN. Then you can do equality checks instead of substring membership and never step onto an int:

# minimal, readable design
books = {
    "1234": {"author": "John", "title": "Book 1", "stock": 1},
    "2345": {"author": "Jim",  "title": "Book 2", "stock": 1},
}

def loan(isbn, author, title):
    rec = books.get(isbn)
    if not rec or rec["author"] != author or rec["title"] != title:
        return False
    if rec["stock"] <= 0:
        return False
    rec["stock"] -= 1
    return True

This keeps related fields together, makes the lookup O(1) by ISBN, and eliminates fragile index math.

I forgot to mention the error message occurs on line 35.

What i am trying to do in the issamebook function is search for the ISBN number of the book and then look at the next 2 items in the list after the ISBN number which has been found and see if they match the author and title of the book. I then am trying to reduce the stock by one as if the person has taken the book out of the library.

I now have this working. Thanks.

First of all, you are mixing spaces and tabs in your indentations, which will srew up most editors. Please use spaces only!

I assume the error happens in the for loop, and it looks like when you reach the integer element in your booklist you are trying to use "in" to compare a string with an integer.

Solution, have your for loop go in steps of 4 like this:
for i in range(0, len(booklist), 4):

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.