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.
jedi_mind_
 
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

or
Code: Select all
x = X
y = X

or
Code: Select all
x = y = X
gene
 
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?

@gene:

That's not an error

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


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: 118
Joined: Fri Mar 08, 2013 2:42 pm
Location: Buenos Aires, Argentina

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.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1390
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 5 guests