CSci 157 Homework #7

Due: April 03, 2025. This assignment is worth 50 points.

Reading. Zelle Chapter 9, Sec 9.1-9.5

Written Problems (30 points).

  1. (6 points) What are coupling and cohesion, and what do they have to do with Michaelangelo? To find out, follow this link to the essay:

    The Art in Computer Programming

    Explain how the authors define these terms and discuss the connection to Michaelangelo in a paragraph or two.
     
  2. (10 points) Beginning with Section 9.10 of ThinkCS-Swarthmore Edition read the descriptions (and try the examples) for 9.10 - 9.15, skipping 9.13.
    Define or explain the following concepts:
    • the is operator
    • aliasing
    • cloning
    • list parameters
    • pure functions
       
  3. (6 points) Draw a state diagram for a and b before and after the third line of the following python code is executed:
       >>> a = [1, 2, 3]
       >>> b = a[:]
       >>> b[0] = 5 
    
  4. (8 points) One way to test your understanding of loops is to try converting one type of loop to another. Consider the following definition:
        maxValue = -1
    goodInput = False

    print("Welcome to the Modulus 7 Demo Program")
    print("-------------------------------------")

    while not goodInput:
    maxValue = int(input("\nEnter a non-negative integer: "))
    if maxValue >= 0:
    goodInput = True

    x = 0
    y = 7
    while x <= maxValue:
    print("\n", x, " %% ", y, " ==> ", x % y )
    x = x + 1
    print()
    • (4 points) Would it make sense to convert the first while loop to a for loop? If so, write out the new for loop. If not, explain why not.
    • (4 points) Would it make sense to convert the second while loop to a for loop? If so, write out the new for loop. If not, explain why not.

Programming Problems (20 points).

Reminder: For all programs, follow the Python coding standards and function design methodology given in the Function Definitions notes. Be sure to start any Python files with a comment or docstring containing your name, the program filename, and a brief description of the program.

  1. (8 points) Download the file pitch.py and run it a few times. This program just creates a ball (a black circle) and sends it rolling across the window. Modify the program in the following ways:
     
    • the ball moves in both the x- and y-directions an amount governed by the variables dx and dy. Randomize these using random.randint, but don't make the ranges very large (they should be centered on the values currently assigned to the variables). The range for the y-coordinate should include both positive and negative values.
       
    • add a function checkBounce that takes the x- and y-coordinates of the ball as parameters and returns True if the ball has hit one of the sides of the window, False otherwise. If calling this function returns True, reverse the direction of movement by multiplying dx or dy by -1 (depending on which side was hit). Also, if the ball hits one of the sides, select a new speed at random, remembering always to reverse the motion.
       
  2. (6 points) Animate your emoji! Develop a program with at least 3 functions: one to draw the emoji, one to move the shapes that make up the emoji as a unit, and a main function to test them out. Be creative about how and when to move the shapes. Add changes to the color scheme: background color, emoji colors, etc. If you'd rather, you can animate my emoji. Review the techniques from the pitch/catch/raceway examples for ideas on how to do this.

    For up to 6 points extra, animate 3 or more emojis at different speeds. One student created a simple starfield and had several UFO-emojis race across it.
     
  3. (6 points) Download and run the provided program WeatherData - the weather data is a set of lists containing temperature readings, rainfall, for a single month, like March. The program defines a set of functions to display various readings for a particular day of the month and a main method for trying them out. Note that several functions are not yet properly implemented.

    Complete the following functions for computing various averages for the given WeatherData, then add calls in the appropriate place in main to run them.
    • averageDailyTemp - given a date (just a number 1-31) as its parameter, compute and return the average temperature for that date
    • averageMonthlyTemp - compute and return the average temperature for the month. You can (and should!) use averageDailyTemp in your solution
    • averageMonthlyRainfall - compute and return the average rainfall for the month

Turning in Your Work

Written Problems. Turn in written exercises one of these ways:

Programming Programs. Post your Python files (the .py files, not screenshots) to the Assignments page on BrightSpace. Be sure your name is in the comment (or docstring) at the top of each file.