def learn_to_program(): return """ CS149 - Introduction to Programming Department of Computer Science James Madison University Spring 2022 """
The goal if this assignment is to gain experience working with:
<
, >
, ==
, etc.)and
, or
, not
)in
)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.
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,
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.
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:
amount
: the dollar amount to be taxed.category
: a string representing the category of the goods to be taxed:
'food'
'baby items'
age
: the age of the customer in years.The function should return the customer’s sales tax in dollars according to the rules above.
Example:
Note that the answers are in dollar amounts, but have long decimals due to imprecision in floating point arithmetic.
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:
get_value
: this function should take a string card value and return the integer value of the card. Numbered playing cards are valued 2-10; Aces are valued as 1 and royal cards (Jack, Queen, and King) are valued at 10.dealer_hits
: this function should take two card string values (card1
and card2
) and return a Boolean result of True if the dealer should hit and False if the dealer should stand. You should call your get_value
function to determine card value for this function. Rules for the dealer hitting are as follows:
True
).False
), as they have a winning hand of 21.You should use the following tuples in setting up your comparisons / values:
= ('0', 'A', '2', '3', '4', '5', '6', '7', '8', '9', '10')
card_tuple = ('J', 'Q', 'K') royal_cards
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.
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...
= (255, 0, 0)
red = (0, 255, 0)
green = (0, 0, 255)
blue = (0, 0, 0)
black = (255, 255, 255) white
Complete the following two functions for working with color values:
adjust_brightness
This function will take two parameters:
the first is a length-three tuple representing a color. The
second is an integer representing a value (positive or negative)
that will be added to each color channel. The return value must
be a tuple where each color channel has been adjusted by the
requested quantity, with values clipped to stay in the range
0-255. For example:
Advice: As you program this function, you will probably find
yourself repeating much of the code three times: once for each
color channel. This is a good opportunity to use a helper
function. Helper functions are functions that simplify the
implementation, but aren’t explicitly required by the
specification. In this case, it would be useful to have a helper
function named adjust_channel
that would handle the logic
for increasing or decreasing the value of a single color. You can
then avoid code duplication by calling this function three times
in adjust_brightness
.
same_color
This function returns a Boolean value indicating
whether two provided color tuples are similar enough to be
considered the same color. It must take three parameters: the
first two are color tuples, and the third is an integer value
representing the maximum allowable difference between
corresponding channels in the two color tuples. For example:
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.
adjust_brightness.py
: This
application allows the user to select an image (which must be in JPG
format), and increase or decrease the brightness by a user-specified
amount. This is accomplished by applying your adjust_brightness
function to each pixel.
green_screen.py
: This application allows
the user to select an image with a green-screen background and a
second image that will be used as a background replacement. It then
applies a green-screen effect by checking each pixel in the original
image against the green-screen color using your same_color
function. Pixels that are determined to be the same color as the green
screen are replaced with the corresponding pixel from the background
image. You can test the application using the following two images.
Right click them and download them into the same folder as your
Python code.
If you want to create your own green-screen images, this web-based image viewer will allow you to view the colors of individual pixels so you can determine the appropriate color values for the “green” in your background: https://imagecolorpicker.com.