For Loop says 'int object is not iterable.'

This is the place for queries that don't fit in any of the other categories.

For Loop says 'int object is not iterable.'

Postby jedi_mind_ » Sun Jun 08, 2014 5:53 pm

Code: Select all
def MakeGrid(X, width=640, height=480):
   x, y = X
   count = 0, 0
   Grid = []
   amount = Width / x, Height / y
   for i in range(amount[0]):
      for o in range(amount[1]):
         Grid.append(pygame.Surface((i*x, o*y)))
   return Grid
Last edited by micseydel on Sun Jun 08, 2014 6:02 pm, edited 1 time in total.
Reason: Locked OP.
Posts: 11
Joined: Sun Jun 08, 2014 5:31 pm

Re: For Loop says 'int object is not iterable.'

Postby gene » Sun Jun 08, 2014 6:51 pm

The error comes from

Code: Select all
x,y = X

which is not allowed in Python.

Code: Select all
x,y = X,X

Code: Select all
x = X
y = X

Code: Select all
x = y = X
Posts: 4
Joined: Sun Feb 24, 2013 8:07 pm

Re: For Loop says 'int object is not iterable.'

Postby Crimson King » Mon Jun 09, 2014 1:49 am

Hi jedi_mind_ welcome to the forum. I just run that code and didn't get the same error as you. Would you mind being more specific with your traceback?


That's not an error

Code: Select all
>>> X = (1,2)
>>> x,y=X
>>> x
>>> y

It's called tuple unpacking.
Last edited by Crimson King on Mon Jun 09, 2014 2:57 am, edited 1 time in total.
User avatar
Crimson King
Posts: 166
Joined: Fri Mar 08, 2013 2:42 pm
Location: Dublin, Ireland

Re: For Loop says 'int object is not iterable.'

Postby micseydel » Mon Jun 09, 2014 1:53 am

Hello jedi_mind_! Welcome to the forum :)

I'd like to second Crimson King's advice, and let you know that you're more likely to get a response if you provide all that, on top of removing extraneous code to your question (for example, Pygame code, or even the rest of the function if it's not relevant to your question). I highly recommend you do all this the best of your ability. If you want to further optimize your question so that it can be answered with less back and forth, try reviewing this.
Due to the reasons discussed here we will be moving to on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 9 guests