Exam 2

3 minute read

Type: Individual Assignment
Due: before class on Thursday, March 18, 2021

Before You Begin

Start by reading the exam procedures page. It covers important policies that are in place during an exam week.

Problem 1: Finishing a Program

Suppose we wish to write a Python program that prompts the user to enter a filename and a keyword and will search for all occurrences of the keyword in the file. The program should print all lines that contain the keyword or display a message that the file does not contain any occurrences of the word.

Below is an implementation of such a program that is unfinished. Finish the program by filling in the blanks below.

def main():
    filename = input("Enter the name of a file: ")
    keyword = input("Enter a keyword to search for: ")

    file = _______________

    # Will set to True if we find the keyword
    found_it = False

    for line in _______________:
        if ______ in ______:
            found_it = True


    if _______________:
        print("No occurrences of", keyword, "were found.")


Place your solution in a file named find_keyword.py.

Problem 2: Creating a Dialog Box

For this problem, put all of your code into a Python file called dialog_box.py. Below you can find a starter file that helps you format your solution:

Part A: Point Containment

When working with graphics, one common operation is to check if a point $(x,y)$ is inside a given shape. In this part, you will write a function is_inside that checks if a Point object is inside a Circle object. In particular, the function should adhere to the following documentation:

def is_inside(p, circ):
    """Checks if a point is inside a circle

        p, a Point object
        circ, a Circle object

        result, a bool

        * The graphics library is installed and imported

        * If the distance between p and the center of circ
          is less than or equal to its radius, then the
          result is True
        * Otherwise the result is False

You may find the following equation helpful, which expresses the distance between two points $(x_1,y_1)$ and $(x_2,y_2)$:

\[d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}\]

It may be helpful to open the the Zelle graphics library reference to see what methods the Point and Circle classes support.

Here are some sample executions:

>>> is_inside(Point(1,1), Circle(Point(0,0),1))
>>> is_inside(Point(0,0.5), Circle(Point(0,0),1))

Part B: Yes/No Dialog Box

Many programs have dialog boxes that pop up to ask the user of the program a question. Occasionally these boxes may ask a question with buttons like “OK” and “Cancel” or even “Yes” or “No”. In this part, you will write a function called yes_no_dialog that creates a graphics window to display a message to the user and returns True if they click “Yes” and False if they click “No”. Your function must adhere to the following documentation:

def yes_no_dialog(message):
    """Opens a yes/no dialog box with the given message

        message, a string

        result, a bool

        * message is short enough to be displayed on a
          single line

        * A GraphWin window appears with the given message
          and two circular buttons labeled "Yes" and "No"
        * The function should only return when the user
          clicks inside the "Yes" or the "No" buttons
        * If they click "Yes", the window closes and the
          result that is returned is True
        * If they click "No", the window closes and the
          result that is returned is False

Here is an example main function that uses your yes_no_dialog function. It firsts asks the user if they like coffee, and if they say no, it then asks if they like tea.

def main():  
    if yes_no_dialog("Do you like coffee?"):
        print("Excellent! I like coffee, too!")
    elif yes_no_dialog("Do you like tea?"):
        print("Tea is great!")
        print("Neither coffee nor tea? Water it is then!")

After running this program, the user should see dialog boxes that look somewhat like the following:

Coffee Dialog

If they click “No”, they should then be presented with the dialog:

Coffee Dialog

Since the buttons corresponding to “Yes” and “No” are circles, you may also find it helpful to use your function is_inside from Part A to check if the user clicked on a button.

Your dialog box can look different than the ones presented above, but they must display the text given in the message parameter and have two circle objects that represent Yes and No.