def learn_to_program():
    return """  
        CS149 - Introduction to Programming
        Department of Computer Science
        James Madison University
        Spring 2022
    """

Homework #6

Objectives

The goal if this assignment is to gain experience working with:

Docstrings

For this homework, and all of the remaining homeworks, all of your modules and functions must exhibit correct documentation. Submissions that don’t pass docstring checks will receive no credit.

You should read the Docstring Requirements carefully to see how it should be done.

Exercise 6.1 Positive or Negative?

If statements can be used to differentiate between different types of numbers. Write a program, sign.py, which contains a function positive_negative, which prints out either:

depending on the sign of the number passed as a parameter.

For example,

>>> positive_negative(35) 35 is positive. >>> positive_negative(0) 0 is neither negative nor positive. >>> positive_negative(-4) -4 is negative.

Exercise 6.2 Even or Odd?

Sometimes our program might get a lot of data and we’d like to sort it into multiple categories. In this problem, you will write a function that sorts integers into even and odd.

Start with the code in even_odd.py. Write the function sort_even_and_odd according to the docstring provided.

Note #1: A number n is even if n % 2 == 0. It is odd if n % 2 == 1. If n % 2 is anything else (i.e., a decimal), then n is a floating point number.

Note #2: The word “sort” can have two distinct meanings. We can sort items into categories, as in “I sorted my white socks into one drawer and my dark socks into another.” Sorting can also describe the process of rearranging a sequence of items into a desired order, as in: “These names need to be sorted alphabetically.” This question uses the first meaning of sort: to place into categories.

Exercise 6.3 Sales Tax

In a certain state, sales tax is computed as follows:

In a file named taxes.py, write a function sales_tax which takes the following parameters in order:

The function should return the customer’s sales tax in dollars according to the rules above.

Example:

>>> sales_tax(100, 'baby items', 46) 3.5000000000000004 >>> sales_tax(53, 'office supplies', 65) 1.3250000000000002

Note that the answers are in dollar amounts, but have long decimals due to imprecision in floating point arithmetic.

Exercise 6.4 Blackjack

The card game Blackjack is played with a standard 52 card deck.  The rules are as follows:

Create a file named blackjack.py. For this exercise you will define two functions:

You should use the following tuples in setting up your comparisons / values:

card_tuple = ('0', 'A', '2', '3', '4', '5', '6', '7', '8', '9', '10')
royal_cards = ('J', 'Q', 'K')

Hint #1: It may seem odd that the first entry in card_tuple is '0', which doesn’t correspond to any card. This is done as a convenience so that the index of each card type corresponds to its value: 'A' is at index 1, '2' is at index 2 etc. This fact can be used to avoid creating a long ugly conditional block in your get_value function.

Hint #2: The code for this problem will be much shorter and cleaner if you take advantage of the membership operators described in section 6.9 of our textbook.

Exercise 6.5 Image Manipulations

Computers typically represent images as a grid of pixels where each pixel is a tiny rectangular region of a particular color. The color of a pixel may be represented as a three-entry tuple where the first entry represents the amount of red, the second represents the amount of green and the third represents the amount of blue. These three color values (often referred to as channels) are restricted to be in the range 0-255. For example:

# Create some variables to store tuples representing common colors...
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
black = (0, 0, 0)
white = (255, 255, 255)

Complete the following two functions for working with color values:

Just for fun: We’ve provided some simple demo programs that you can use to try out these functions once you have them working. In order to execute these programs, store then in the same directory as your color_utils.py file and run them using Thonny. You will need to install the Pillow (Python Imaging Library) package through the Thonny package manager.