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:
********************
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!
********************
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("<img src=\"/resources/{}\">".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!
********************