CSci 157 Homework 3
Due: February 13, 2025This assignment is graded out of 50 points. For full credit:
- Assignments are due by class time on the due date
- Assignments are to be pledged and done individually
- See the Turning in Your Work section below
Written Problems (26 points). For the following problems, write out your answers by hand or in a text editor.
- (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
- initialize a variable called
- (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') - (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") - (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:
- (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
andgetHeight
, that do what you'd expect. Wait for a mouse click before closing the window and ending the program.
- (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.
- (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.
- (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:
- complete the assignment on paper and turn in at the beginning of class on the due date
- complete the assignment in a text editor and post to the Assignments page on BrightSpace by the due date
- post your Python files to the Assignments page either individually are combined into a Zip file
- in each case, make sure your name is clearly visible