CSci 157 Homework 4

Due: February 20, 2025

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

Reading.  Zelle, Chapter 7 Sections 7.1 - 7.7

Written Problems (30 points). For the following, write out your answers on a separate sheet of paper by hand or using a text editor.
  1. (1 points each: 8 pts) For each statement below:
    First, determine the result without running Python (Illegal syntax is a possible result).
    Second, evaluate the expression in Python and give the result.
    Are any of the results surprising? Put a star (or asterisk) next to any results that didn't work as you expected.
     
    First assign x the value 2, y the value 7, and z the value -3.
     
  2. (4 points) Give the truth table for the following logical expression: 
    not (p or q)
  3. (12 points) Read through Zelle Section 7.2 (also on BrightSpace), which shows ways to use selection statements to improve a program quadratic.py that computes the real-valued roots of a binomial equation using the quadratic formula from Algebra. The trouble is, the program fails in some cases.
     
    Test the original program, quadratic2.py and quadratic3.py (both found in Section 7.2), and a new version quadratic4.py with the values shown in the table. Enter the numbers shown in order as you are prompted for each coefficient. Record your results in a file called Problem3.TESTS (this filename works fine on Linux; on other systems, you may need to add a .txt file extension).
     
    Program Test for real roots Test for no real roots
    quadratic.py 1, 0, -9
    1, -2, 1
    1, 2, 3 (should fail)
    quadratic2.py 1, 0, -9
    1, 2, 1
    1, 2, 3
    quadratic3.py 2, 4, 1
    1, 2, 1
    1, 3, 9
    quadratic4.py 1, 2, 1
    2,-5, 3
    1, 2, 3

     
  4. (6 points) Do comparison operators work with strings in Python? What other operators give boolean results? Try each of these string operations in the shell and record the results:
    'apple' > 'pineapple'
    'apple' > 'Pineapple'
    'Alligator' < 'zebra'
    'alligator' < 'zebra'
    'apple' in 'Pineapple'
    'zebra' in 'alligator'
    Briefly explain why you get each result.

Programming Problems (20 points).

  1. (8 points) Write a program that creates a window with two non-overlapping circles in it, each a different color. After drawing the shapes, wait for the mouse to be clicked and save the location of the click in a variable). Now determine if the mouse was clicked in one of the circles (how can you do this?). If so, change the color of the shape to red (if already red, change the shade of red, just so we can tell it changed). If the click was outside both circles, remove them from the window.
    Wait for another mouse click before closing the window.
     
  2. (8 points)  Return to the program quadratic4.py from Problem 3 of the Written Part above. There is still one input that can cause the program to crash with an error, can you see what it is? Hint: what happens if we divide by 0? Try the program with a = 0 and record the result in Problem3.TESTS.

    Now add a selection statement to check that the first coefficient is not 0 before continuing (this only makes sense: if the first coefficient is 0, the equation isn't a quadratic now is it?).
    If a is 0, print Error: Not a valid quadratic! Decide whether this check can go into the existing if structure or if you should add a whole new if-statement.
     
  3. (4 points) Make sure your modified quadratic4.py program passes all tests from Written Problem 3, and add at least 4 new tests, including at least one with a = 0.
    Record these tests and the results you obtain in Problem3.TESTS, being sure to label the new section so it's easy to see which tests go with which program.
    Note
    : don't make up quadratic equations of your own without knowing the roots! You should always know the results of your tests before trying them.

Turning in Your Work

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

Programming Programs. Post your Python files and test results to the Assignments page on BrightSpace either individually or combined into a single Zip file. Be sure your name is in a comment describing the program at the top of each file, like this:

''' quadratic4.py - computes quadratic formula
    modifications due to S. Carl '''