Homework Solution

cs1graphics warmup

Problems to be Submitted (18 points)

  1. The class Canvas is not built-in to Python. It is part of our auxilary cs1graphics module, and can only be used after that has been imported, namely after a command such as:

    	from cs1graphics import *
    	

  2. The syntax of the statement

            sq.setFillColor(Red)
    	
    expects the term Red to be an identifier, presumably for an object already created earlier in your program. Note carefully, that this is different from the syntax "Red" which automatically refers to a string object with the three characters R e d. The quotation marks are key in how Python interprets the syntax.

    So we might either write:

            sq.setFillColor("Red")
    	
    or equivalently
            sq.setFillColor('Red')
    	
    as string in Python can be delimited by single or double quotation marks.

    Alternatively, we could make the original syntax legal by first defining the identifier Red to a meaningful value. We could set it to a string and use it as

    	Red = "Red"             # This makes Red reference the string "Red"
            sq.setFillColor(Red)
    	
    or we might decide to use an RGB value for a particular shade of red that we like, different from the standard red color.
    	Red = (240, 10, 18)     # our favorite shade of red
            sq.setFillColor(Red)
    	

    It is worth noting that if following standard coding conventions, we would typically not use a capitalized word, such as Red, as an identifier to refer to a value. We would prefer to use the name red for such a variable.

  3. The problem here involves the assignment operator. Though two disinct Rectangles were instantiated along the way, by the time the two lines

            can.add(rect)
            can.add(rect)
    	
    are executed, the reference variable rect is a reference to one particular object, namely the second of the two rectangles to have been created. The first rectangle was not added in this case. There are two possible solutions, one would have been to add the first rectangle earlier in the sequence, when variable rect still references that rectangle. Another approach would be to use two distinct identifiers (e.g. rect1 and rect2) to name the two rectangles.


Last modified: Thursday, 26 January 2017