Re: [Tutor] Weird Python Error
On 17/10/13 21:09, Zaid Saeed wrote: class YellowCar(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image = pygame.image.load("yellow.png") self.image = pygame.transform.scale(self.image,(50, 75)) self.rect = self.image.get_rect() def update(self): self.rect.centery += self.dy if self.rect.top > screen.get_height(): self.reset() def reset(self): self.rect.bottom = 0 self.rect.centerx = random.randrange(190, screen.get_width()) self.dy = random.randrange(10, 30) Update uses dy but it is only set in reset(). If you call update before reset you will get the error. Now in your Game() code we find: if hitCar: redCar.sndThunder.play() scoreboard.lives -= 1 if scoreboard.lives <= 0: keepGoing = False for theCar in hitCar: theCar.reset() # allSprites.clear(screen, background) goodSprites.update() badSprites.update() scoreSprite.update() So any updated car not in hitcar will not have been reset and so you will have an error. It's best (for your sanity) to initialise all class attributes in __init__. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Nested lists help
Hi, I'm trying to create a 3D grid, so I'm using a list of list of lists. However, if I try to make a change to one value, eggrid[0][1][2] = 3, It doesn't just change list[0], it change the 2nd element in the 1st list of every list? I think it's because I've copied lists to get the grid, but could you help me fix this? This is my code, that takes an x,y,z value from user. self.grid_x = xself.grid_y = yself.grid_z = z self.grid = []self.grid2D = [] for i in range(self.grid_y):row = [0]*x self.grid2D.append(row) for k in range(z):self.grid.append(self.grid2D) #Creates list of x by y grids Thank you so muchCorrine ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Nested lists help
On 18-Oct-2013, at 17:13, Corinne Landers wrote: > self.grid_x = x > self.grid_y = y > self.grid_z = z > > self.grid = [] > self.grid2D = [] > So here you create a list, self.grid2D. > for i in range(self.grid_y): > row = [0]*x > self.grid2D.append(row) > Here you are adding more elements to that list. > for k in range(z): > self.grid.append(self.grid2D) #Creates list of x by y grids > But here you keep appending self.grid2D multiple times to self.grid. Not z different copies, but they're all the exact name list object, referenced multiple times.___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Nested lists help
On 18/10/2013 20:13, Corinne Landers wrote: > --> > Hi, I'm > trying to create a 3D grid, so I'm using a list of list of > lists. However, if I try to make a change to one value, > eggrid[0][1][2] = 3, It doesn't just change > list[0], it change the 2nd element in the 1st list of every > list? I think it's because I've copied lists to get the grid, > but could you help me fix this? This is my > code, that takes an x,y,z value from > user. self.grid_x = > xself.grid_y = yself.grid_z = > zself.grid = []self.grid2D = > []for i in range(self.grid_y): > row = [0]*x > self.grid2D.append(row)for k > in range(z): self.g rid.append(self.grid2D) #Creates list of x by y gridsThank you so muchCorrine > > > Please don't post using html mail. Use text email for a text newsgroup. Not only does it waste space (since the message essentially has two copies of what you type, the html part being much larger than what you typed), but it also can frequently lose all the indentation (as happened to this message in your email program). Indentation matters in Python. Your lines are also run together in places, like the first line of your code, which looks like this: self.grid_x = xself.grid_y = yself.grid_z = z The problem you have, as you have guessed, is that you're making references to the same list multiple times. So when that list is changed, it changes all references. You need to have every item in your nested list be unique. The simplest change is to use myotherlist = mylist[:] to make a copy of each element of the list. That's not necessary at the innermost level, since integers are immutable. I'm not sure what to make of your code, since you say you want a 3d grid, but you're only making 2d. But to make a 3d list, you might use something like (untested): my_3d_list = [] for i in range(x): temp1 = [] for j in range(y): temp2 = [] for k in range(z): temp2.append(0) temp1.append(temp2) my_3d_list.append(temp1) The innermost loop could be replaced by temp2 = [0]*z but I like the symmetry of the way I wrote it. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor