CSci 157 Homework 3

Due: February 13, 2025

This assignment is graded out of 50 points. For full credit:

Reading. ThinkCSPY Chapter 7, Sections 7.1 through 7.7; complete the activity questions for each section. For the programming problems, refer to Zelle Graphics

Written Problems (26 points). For the following problems, write out your answers by hand or in a text editor.
  1. (2+2+2 points) Show how to do these assignments:
    • initialize a variable called count with the integer value 15
    • initialize a variable called nextFriday with the string value "Valentine's Day"
    • initialize a variable called origin to be a Point with x-coordinate 0 and y-coordinate 0
       
  2. (6 points) Assume you have two integer variables temp1 and temp2, initialized to -1 and 42, respectively. Without running them in the Python shell, show what would be output by the following statements (at least one results in an error):
       print('The temperature is ' + temp2 + 'degrees')
    print('The temperature is ' + str(temp2) + 'degrees')

    print('The temperature is ' + (temp1 + temp2) + 'degrees')
    print('The temperature is ' + str(temp1 + temp2) + 'degrees')

    print('The temperature is ' + temp2 - temp1 + 'degrees')
    print('The temperature is ' + str(temp2 - temp1) + 'degrees')
  3. (2 points) Now try these statements in the Python interpreter and record the output. These example use f-strings (formatted string literals) first introduced in Python 3.6:
       temperature = -1
    thatCount = 42
    print(f"The temperature is {temp1 + temp2} degrees")
    print(f"The temperature is {temp2 - temp1} degrees")
  4. (12 points) Rewrite each expression below using parentheses to show the order in which each operator will be evaluated. Then, say if the expression evaluates to True or False (2 points each).

    Assume the value of x is 2, y is 4, and z is -3.
    • x + 2 > z
    • x > 1 and x > 3
    • 1 < x or y < 3
    • not(x + y < 10 or z + 1 > -1)
    • x - 6 < y or z == -2 * x + 1
    • x - 6 < y and z == -2 * x + 1

Programming Problems (24 points). The purpose of this section is to practice developing and testing short programs. Include a comment at the top of each file with your name and the assignment number. After having a look through the Graphics Reference, write Python scripts to perform each task:

  1. (6 points) Create a GraphWin, then create and position a 80 by 50 Rectangle centered in the window, no matter dimensions you choose for the window. Hint: GraphWin defines two methods, getWidth and getHeight, that do what you'd expect. Wait for a mouse click before closing the window and ending the program.
     
  2. (6 points) In the same program, create a Text object saying "Centered Here!" in italics, centered in the window, no matter the window dimensions; if the text extends outside the Rectangle  use a smaller font. You may need to call one or more Text methods after it has been created. Wait for a mouse click before closing the window and ending the program.
     
  3. (4 points) New program: create a GraphWin, then create and draw a circle anywhere in the window. Use yellow as its fill color and red as its border. Wait for a mouse click, then "undraw" the circle. A second mouse click ends the program.
     
  4. (8 points - challenge problem) Write a new Python script that opens a window and draws 10 vertical lines across it, equally spaced apart, like bars. Use the width of the window to determine how far apart the 10 lines should be. Each line should be at least 5 pixels thick. Use a for loop to simplify the code - you might want to review the section in Zelle about definite loops (Section 2.6).

    Change the background of the window to any color you like. Wait for a mouse click, then close the window. 

Turning in Your Work. You can turn in written exercises one of these ways: