CSC111 Lab 7 Moodle 2015
--D. Thiebaut (talk) 17:49, 8 March 2015 (EDT)
<showafterdate after="20150311 12:00" before="20150601 00:00">
This page contains the information regarding the Moodle submission for CSC 111 Lab #7. You have until 3/13 11:00 a.m. to submit your program.
Moodle Submission
- Write python program called lab7.py that contains only the following functions you wrote for the last challenges of Lab 7:
- isLeftSide()
- isUpperHalf()
- getColor()
- Your program will contain only these 3 functions. No main function. No graphics required. Your program will be called from a test program that will test your three functions separately.
Test Program
- If you want to test your lab7.py using a similar test used by Moodle, simply create the program below in the same directory where you have stored lab7.py, and run it. It will test your lab7.py functions for you.
# testLab7.py
# D. Thiebaut
# This program tests all the functions for Lab 7.
# To use this program, simply write a program called lab7.py and
# store your functions in it. The functions are expected to be
# named isLeftSide(), isUpperHalf(), and getColor().
# To test the the function, simply make sure that testLab7.py and
# lab7.py are in the same directory, and run testLab7.py. It will
# automatically import lab7.py and call the different functions,
# passing them parameters and checking the values returned by
# the functions.
import sys
import random # that's the module that allows us to pick random quantities
import lab7 # that's your hw6.py module. We don't add ".py" to modules,
# but the file containing the program must have ".py" at the end.
# ---------------------------------------------------------------------
def testIsLeftSide():
try:
score, total = testIsLeftSideb()
return score, total
except:
e = sys.exc_info()[0]
print( e )
print( "isLeftSide does not exist or crashed. 0 out of 8 points." )
return 0, 8
def testIsLeftSideb():
xyw = [ (0, 100, 20, True), (9, 100, 20, True), (11, 100, 20, False), (20, 100, 20, False ),
(0, 100, 100, True), (49, 100, 100, True), (51, 100, 100, False), (100, 100, 100, False ) ]
noTests = len( xyw )
numberGood = 0
for i in range( noTests ):
x, y, w, expected = xyw[i]
try:
userOut = lab7.isLeftSide( x, y, w )
except:
e = sys.exc_info()[0]
print( "isLeftSide crashed:", e )
return numberGood, noTests
if type( userOut ) != type( expected ):
print( "isLeftSide does not return a boolean. 0 out of", noTests )
return 0, noTests
if expected == userOut:
numberGood = numberGood + 1
else:
print( ("isLeftSide( %d, %d, %d ) returns" % (x,y,w)), userOut, "instead of", expected )
print( "isLeftSide passes", numberGood, "out of", noTests, "tests." )
return numberGood, noTests
# ---------------------------------------------------------------------
def testIsUpperHalf():
try:
score, total = testIsUpperHalfb()
return score, total
except:
e = sys.exc_info()[0]
print( e )
print( "isUpperHalf does not exist or crashed. 0 out of 8 points." )
return 0, 8
def testIsUpperHalfb():
xyw = [ (100, 0, 20, True), (100,9, 20, True), (100, 11, 20, False), (100,20, 20, False ),
(100, 0, 100, True), (100, 49, 100, True), (100, 51, 100, False), (100, 100, 100, False ) ]
noTests = len( xyw )
numberGood = 0
for i in range( noTests ):
x, y, w, expected = xyw[i]
try:
userOut = lab7.isUpperHalf( x, y, w )
except:
e = sys.exc_info()[0]
print( "isUpperHalf crashed:", e )
return numberGood, noTests
if type( userOut ) != type( expected ):
print( "isUpperHalf does not return a boolean. 0 out of", noTests )
return 0, noTests
if expected == userOut:
numberGood = numberGood + 1
else:
print( ("isUpperHalf( %d, %d, %d ) returns" % (x,y,w)), userOut, "instead of", expected )
print( "isUpperHalf passes", numberGood, "out of", noTests, "tests." )
return numberGood, noTests
# ---------------------------------------------------------------------
def testGetColor():
if True: #try:
score, total = testGetColorb()
return score, total
else: #except:
e = sys.exc_info()[0]
print( e )
print( "getColor does not exist or crashed. 0 out of 8 points." )
return 0, 8
def testGetColorb():
xywl = [ (20, 20, 100, 100), (80, 20, 100, 100), (20, 80, 100, 100), (80, 80, 100, 100 ),
(20, 20, 200, 200), (180, 20, 200, 200), (20, 180, 200, 200), (180, 180, 200, 200) ]
noTests = len( xywl )
numberGood = 0
colors = []
for i in range( noTests ):
if i%4 == 0:
colors = []
x, y, w, h = xywl[i]
try:
userOut = lab7.getColor( x, y, w, h )
except:
e = sys.exc_info()[0]
print( "getColor crashed:", e )
return numberGood, noTests
if type( userOut ) != type( " " ):
print( ("getColor does not return a string when passed %d, %d, %d, %d." %
(x, y, w, h)), ("%d out of %d" % ( numberGood, noTests ) ) )
return numberGood, noTests
if not userOut in colors:
colors.append( userOut )
numberGood = numberGood + 1
else:
print( ("getColor( %d, %d, %d, %d ) returns" % (x,y,w,h)),
userOut, "which was already returned for another corner.\n",
"There should be 4 distinct colors for the 4 corners." )
print( "getColor passes", numberGood, "out of", noTests, "tests." )
return numberGood, noTests
def main():
score = 0
total = 0
numberGood, maxGood = testIsLeftSide()
score = score + numberGood
total = total + maxGood
numberGood, maxGood = testIsUpperHalf()
score = score + numberGood
total = total + maxGood
numberGood, maxGood = testGetColor()
score = score + numberGood
total = total + maxGood
print( "\nYour functions pass {0:1} tests out of {1:1}."
.format( score, total ) )
main()
</showafterdate>
<showafterdate after="20150313 11:00" before="20150601 00:00">
Solution Program
# lab7.py
# D. Thiebaut
# 3 functions that hat to be written for Lab #7
#
def isLeftSide( x, y, width ):
if x < width//2:
return True
else:
return False
def isUpperHalf( x, y, height ):
if y < height//2:
return True
else:
return False
def getColor( x, y, w, h ):
if x < w/2 and y < h/2:
return 'yellow'
elif x < w/2 and y > h/2:
return 'blue'
elif x > w/2 and y < h/2:
return 'red'
else:
return 'brown'
</showafterdate>