Assignment 5

5 minute read

Type: Group Assignment

Prologue

This is a group assignment. Be sure to review our Code of Conduct for pair programming and be careful to adhere to it when working on the assignment. Keep in mind that you both must be authors of all code submitted.

Problem 1: Describing Numbers

Write a program describe_number.py that asks the user for a number and then prints a description of that number. The description of the number should adhere to the following rules.

  • If it is less than -1,000,000 include very small in the output
  • If it is more than 1,000,000 include very large in the output
  • If it is negative, include negative in the output
  • If it is positive, include positive in the output
  • If it is even, include even (zero is considered even)
  • If it is odd, include odd in the output
  • If it is of type int, include number that is an int in the output
  • If it is of float type, include number that is a float in the output

For example, here is a table of some inputs and their corresponding outputs.

Input Output
56 positive even number that is an int
-101.0 negative odd number that is a float
23.7 positive number that is a float
1000001 very large positive odd number that is an int

This problem can get rather complicated if you “hard code” every possible case into a long series of conditionals. Instead, try to identify which tasks are independent of other tasks. For example, the positive / negative aspect of the number is independent of the int / float aspect of the number. Thus, you can determine these separately and combine your answer at the end.

Hint: Read input from the user using eval(input()) on this problem. (Do not do this in general, but it is convenient for this problem.) If they type in a float, then it will be given a “float” type and if they type in an int, it will be given an “int” type. Then you can check if a variable val is of type int by using the Boolean expression: type(val) == int

Problem 2: Whatzitdo?

Sometimes programmers write functions that are difficult to read. Let’s take a look at the following function.

def apple(orange, banana):
    """Given an orange and a banana, will return a salad"""
    salad = ""
    pear = 0
    for i in range(orange):
        salad = salad + banana
        pear = pear + 1

    return salad

a. Determine what the function is doing and give it a more appropriate name.

b. The variable names aren’t very helpful. Give them names that are more descriptive of what they are.

c. Clean up the function by removing any unnecessary code that does not serve a purpose.

d. Write a more appropriate docstring for the function and include the following sections

  • A short one-line purpose statement
  • A Parameters section listing the types of the parameters
  • A Returns section briefly summarizing the return value and its type
  • A Preconditions section if there are more restrictions on the input other than their type
  • A Postconditions section that describes, in more detail, the exact relationship between the output and the inputs

e. Place the updated function from parts a-c above, along with its documentation in a file named mystery_solved.py

Problem 3: Color Chooser

Many programs include a “color chooser” feature that presents a user with an array of colors, waits for them to click the desired color, and then uses that color in some way. In this problem, you will write a function named pick_color that could be used to such effect.

Begin by copying and pasting the following code into a file named color_chooser.py:

from graphics import *

def pick_color():
    """Asks the user to select a color with their mouse

    Returns:
        color, a string

    Postconditions:
        * A graphics window with several color
          options appears
        * Once the user clicks a color, the graphics
          window closes
        * Returns a string representation for the
          color selected"""
    pass # TODO: Replace this line with your solution

def main():
    print("Pick a color on the window.")
    color = pick_color()
    print("The color you picked is:", color)

main()

When the pick_color function is called, a window like the following should appear.

Pick Color Window

Once the user clicks on the window, the graphics window should immediately close and the function should return the name of the color selected.

For example, if the program above is called and the user clicks in the upper right corner, the result should be:

Pick a color on the window
The color you picked is: cyan

The choice of colors and their arrangement is up to you, but the function should successfully query the user to select a color and return the color they selected with their mouse.

Hints

  1. Remember that you can change the coordinate system using the setCoords method on the graphics window. If you are using a 3x2 grid like the one above, I recommend changing the coordinates to have (0,0) be the lower left and (3,2) be the upper right.
  2. You can wait for the user to click using the getMouse() method on the graphics window. It returns the location of where the user clicked as a Point object.
  3. You can close a graphics window using the close() method.
  4. You may find it helpful to create additional helper functions, but it is not required.

How to Turn in Your Code

  1. Once you are finished with the assignment, you should go to https://codepost.io and log into your account.
  2. Go to the CS 65 course.
  3. Go to Assignment 5 and upload all of your files.
  4. After you submit the assignment, you should be able to reopen it and see a Partners tab at the top of the submission page. Send the link to your partner and have them open it while logged in with their account.
  5. That’s it! You’ve just submitted your assignment.

Important Grading Criteria

I will evaluate your work with the following criteria in mind.

  1. Correctness. Does your code do what is asked? (e.g. named correctly, input and outputs correct values, etc.)
  2. Formatting. Is your code formatted so that it is easy to understand?
  3. Elegance. Is your code concise and elegant?

Partner Evaluation

After turning in your assignment, please fill out the following partner evaluation form. To do so, you will need to log into Google with your Drake username and password, and open the questionnaire.