##### Need help with a checkerboard algorythm
 Need help with a checkerboard algorythm giladal Programmer named Tim Posts: 16 Threads: 8 Joined: Aug 2020 Reputation: 0 Sep-26-2020, 02:58 PM Hey Guys, In the middle of a python course and unfortunately the course is off the air right now so I cant get help from the instructors. Hopefully I can get some help here. The chess() function gets a number in this case n that will determine the size of the canvas. For example: 8 x 8. THe function is supposed to color the pixels either black or white in order to create a checkerboard. As you can see, i tried using (x + y)%2 to determine what pixel to color but it doesnt seem to accept my answer. Plus if I show the image result it prints out very very small... Anyways, would appreciate some help to solve this: ```### DO NOT MODIFY THIS CODE ### ### THIS CODE LETS YOU VIEW THE IMAGE YOU MAKE ### ### USING im.show() ### from PIL import Image import urllib import random def display_image(im): fn = str(random.randint(1, 500)) + ".bmp" im.save(fn) print("".format(fn)) print("") Image.Image.show = display_image ### YOU CAN MODIFY BELOW THIS LINE ### def chess(n): img = Image.new('L',(n,n),255) mat = img.load() for x in range(n): for y in range(n): if (x + y)%2: mat[x,y] = 0 else: mat[x,y] = 255 img.show() return img ### TESTS ### print("********************") print("Starting the test:") print("********************") print("Checking for n = 2") ans = chess(2) test = [0, 255, 255, 0] if type(ans) == Image.Image and list(ans.getdata()) == test: print("CORRECT: A proper chess board was drawn for n = 2") else: print("WRONG: An improper chess board was drawn for n = 2") print("********************") print("Checking for n = 3") ans = chess(3) test = [0, 255, 0, 255, 0, 255, 0, 255, 0] if type(ans) == Image.Image and list(ans.getdata()) == test: print("CORRECT: A proper chess board was drawn for n = 3") else: print("WRONG: An improper chess board was drawn for n = 3") print("********************") print("Tests concluded, add more tests of your own below!") print("********************")```The answers I receive are: ******************** Starting the test: ******************** Checking for n = 2 WRONG: An improper chess board was drawn for n = 2 ******************** Checking for n = 3 WRONG: An improper chess board was drawn for n = 3 ******************** Tests concluded, add more tests of your own below! ******************** Reply bowlofred Giant Foot Posts: 973 Threads: 3 Joined: Mar 2020 Reputation: 120 Sep-26-2020, 09:55 PM Your parity is off. If you change the conditional to `(x + y + 1)%2`, it works. Or you could change your tests to be the reverse. That should work as well. Reply giladal Programmer named Tim Posts: 16 Threads: 8 Joined: Aug 2020 Reputation: 0 Sep-27-2020, 10:41 AM (Sep-26-2020, 09:55 PM)bowlofred Wrote: Your parity is off. If you change the conditional to `(x + y + 1)%2`, it works. Or you could change your tests to be the reverse. That should work as well. Wow, thank you very much for your help. It did indeed solve my issue. Does that mean that changing the color to 255 and leaving it (x+y)%2 the color on the else statement to 255 will also help? ```for y in range(n): if (x + y)%2: mat[x,y] = 255 else: mat[x,y] = 0``` Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Truck Speedometer algorythm Roger454 4 603 Mar-19-2020, 01:40 AM Last Post: Roger454

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020