[Tutor] Fw: Traceback
niyanaxx95 at gmail.com
niyanaxx95 at gmail.com
Tue Nov 18 22:46:28 CET 2014
I get this message:
Traceback (most recent call last):
File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 87, in <module>
File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 20, in main
File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 70, in tilesForSize
builtins.TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'
Sent from Windows Mail
From: Ni'Yana Morgan
Sent: Tuesday, November 18, 2014 4:42 PM
To: tutor at phython.org
Trying to figure out why I get a traceback on line 20, 70, and 87. Please Help!!!!
Problem Statement:
1. The first and last tile in the first row shall be black.
2. The first and last tile in the first column shall be black.
3. The tiles will alternate between black and lemon
The task is to write a function to compute and print:
the number of tiles needed in the first row (the number of columns)
the number of tiles needed in the first column (the number of rows)
the total number of tiles needed to cover the floor
the gap at the end of each row
the gap at the end of each column
And then print (using a function):
1. The colored tile pattern to a graphics window
a. The pattern should be printed centered in the graphics window, with equal gaps at
the end of each row, and equal gaps at the end of each column
Use functions to:
1. Read and validate the input values
a. Width
b. Length
c. Tile Size
2. Compute the number of rows, columns, total tiles required, and the size of the gaps at the
end of each row and column
3. Print the tile pattern
My Code:
# Import graphics from module
from graphics import GraphicsWindow
tileSize = 0
def main() :
# Define global variables
tilesInRow = 0
tilesInCol = 0
gapX = 0
gapY = 0
# Input room width
roomWidth = getNumber(100, 500, "Enter a room width between 100 and 500: ", "")
roomLength = getNumber(100, 450, "Enter a room length between 100 and 450: ", "")
tileSize = getNumber(20, 50, "Enter a tile size between 20 and 50: ", "")
numCols = tilesForSize(roomWidth)
print("The total number of Columns:", numCols)
numRows = tilesForSize(roomLength)
print("The total number of Rows:", numRows)
# Print the total number of tiles
print("The total number or Tiles: %d" %(numCols * numRows))
# Calculate the gap
# the gap = (the total width - the number of tiles * tile width / 2
gapX = calculateGap(roomWidth, numCols)
gapY = calculateGap(roomLength, numRows)
# Print the gaps
print("The gap at each end of a row is: %.1f" % (gapX))
print("The gap at each end of a column is: %.1f" % (gapY))
# Draw graphics window
win = GraphicsWindow(roomWidth, roomLength)
canvas = win.canvas()
# Draw the checkered surface
for row in range(numRows) :
# If the row is even
if row % 2 == 0 :
# If the column is even set color to black, if odd yellow
drawRow(canvas, row, gapX, numCols, gapY, "black", "yellow")
# If the row is odd
else:
# If the column is even set color to yellow, if odd black
drawRow(canvas, row, gapX, numCols, gapY, "yellow", "black")
win.wait()
def getNumber(minBound, maxBound, msg, err_msg) :
num = minBound - 1
while num < minBound or num > maxBound :
if(msg == "") :
num = float(input("Enter a number between %f and %f: " % (minBound, maxBound)))
else :
num = float(input(msg))
if num < minBound or num > maxBound :
if err_msg == "" :
print("Invalid input.")
else:
print(err_msg)
def tilesForSize(size) :
pairs = int((size - tileSize) // int(2 * tileSize))
num = int(1 + (2 * pairs))
return num
def calculateGap(size, num) :
return (size - num * tileSize) / 2
def drawRow(canvas, row, gapX, numCols, gapY, color1, color2) :
for col in range(numCols) :
if col % 2 == 0 :
canvas.setColor(color1)
else:
canvas.setColor(color2)
# Draw the actual rectangle
canvas.drawRect(row * tileSize + gapX, col * tileSize + gapY, tileSize, tileSize)
return
main()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20141118/e51ecba4/attachment-0001.html>
More information about the Tutor
mailing list