[Tutor] Tile Code Program

niyanaxx95 at gmail.com niyanaxx95 at gmail.com
Fri Oct 10 17:46:09 CEST 2014


This is the prompt: Write an algorithm / pseudocode that:
• Reads in a two integers; the width and length of a room.  
i. The valid range for the width (x-axis) of the room is between 100 and 1000 pixels.  
ii. The valid range for the length (y-axis) of the room is between 100 and 900 pixels.
• Validate the inputs, prompting the user to try again if they enter an invalid integer.
• Compute the number of tiles in each row (the number of columns)
• Compute the number of tiles in each column (the number of rows)
• Print the number of columns
• Print the number of rows
• Print the total number of tiles needed
• Print the gap at the end of each row
• Print the gap at the end of each column
• Print the pattern of tiles centered in a graphics window


My Code: #This is a program to compute  tiles 


# Constants #


from graphics import GraphicsWindow


TILESIZE = 20
roomWidth = 100.0
roomLength = 90.0


while roomWidth < 100 or roomWidth > 1000:
   roomWidth = float(input("Please enter a room width between 100 and 1000:  "))
   if roomWidth >= 100 or roomWidth <= 1000:
      print("Invalid entry")


while roomLength < 100 or roomLength > 900:
   roomLength = float(input("Please enter a room length between 100 and 900:  "))
   if roomLength >= 100 or roomLength <= 900:
      print("Invalid entry.")
   


win = GraphicsWindow(roomWidth, roomLength)
canvas = win.canvas()


#Calculate the number of pairs of tiles
# the number of pairs = interger part of (total width - tile width) / (2 * tile width)


numberOfPairsWidth = int((roomWidth - TILESIZE) // (2 * TILESIZE))
numberOfPairsLength = int((roomLength - TILESIZE) // (2 * TILESIZE))


#Calculate the number of columns and rows


numberOfCol = int(1 + (2 * numberOfPairsWidth))
numberOfRow = int(1 + (2 * numberOfPairsLength)) 


#Calculate the gap
# the gap = (the total width - the number of tiles * tile width / 2


gapCol = (roomWidth - numberOfCol * TILESIZE) / 2
gapRow = (roomLength - numberOfRow * TILESIZE) / 2


# Draw Tiles
for i in range(numberOfCol) : 
    if i % 2 == 0 :
        for j in range(numberOfRow) :
            if j % 2 == 0 :
                canvas.setColor("black") 
            else :
                canvas.setColor("yellow")
            canvas.drawRect(gapCol + i * TILESIZE, gapRow + j * TILESIZE, TILESIZE, TILESIZE)
    else :
        for j in range(numberOfRow) :
            if j % 2 == 0 :
                canvas.setColor("yellow") 
            else :
                canvas.setColor("black")
            canvas.drawRect(gapCol + i * TILESIZE, gapRow + j * TILESIZE, TILESIZE, TILESIZE)


# Print results
print("The number of columns is: ", numberOfCol)
print("The number of rows is: ", numberOfRow)
print("The total number of tiles needed is: ", numberOfCol * numberOfRow)
print("The gap at the end of each rows is: ", gapRow)
print("The gap at the end of each column is: ", gapCol)



The output: Please enter a room length between 100 and 900:  200
Invalid entry.
The number of columns is:  5
The number of rows is:  9
The total number of tiles needed is:  45
The gap at the end of each rows is:  10.0
The gap at the end of each column is:  0.0






Sent from Windows Mail
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20141010/a3f7eed2/attachment-0001.html>


More information about the Tutor mailing list