Practice with Logic and Comparisons
You should have already watched the videos and taken the quiz that introduce this material. As always, at the end of class, submit the CodeSkulptor URL for what you have completed.
As always, work with a partner on the following exercises. Each work on your own computer (assuming enough people bring laptops), but collaborate with your partner. Assignment 1 pairs have been announced, so it would be a good idea to work with that person today and introduce yourself.
Also, use appropriate documentation strings and comments, as described in the Programming Tips video.
Differential Pricing
Differential pricing is the practice of selling identical items to different customers for different prices. Traditional common examples include “student discounts”, negotiated car prices, and higher food prices in Alaska and Hawaii.
But the practice has been in the news numerous times in recent years due to more controversial examples. Staples.com will show lower prices to customers who are known to be sufficiently close to a competitor's store. Amazon.com has shown different prices to customers based upon demographics and how the customer arrived at their site.
In the following two exercises you'll implement two versions of differential pricing. You work for CheapBooks.com, whose main brick-and-mortar competitor is ReadThis. ReadThis has only one Houston-area location — in Spring, TX (a northern suburb).
In each of these, try to keep the code as simple as possible. However, correctness is more important than brevity.
-
Write a function called
easiestdiet_price1(zip)
which returns the displayed price of the best selling book “The Easiest Diet” when the customer comes from the given ZIP Code. The inputzip
is a number.If the customer lives in 77373, the heart of Spring, they see the price $9. If the customer live in any of 77388, 77379, 77386, 77380, 77389, 77381, or 77382, which are considered to be in or near Spring, the customers sees the prices $9.50. Otherwise, the price is $10.
-
Next, we decide to try a more sophisticated approach. If the customer is known to be a frequent customer, we will assume that they'll favor us over our competitor — probably a dangerous assumption.
Write a function called
easiestdiet_price2(zip, frequent)
. The inputfrequent
is a Boolean. It behaves like the previous function, except that frequent customers living in the “nearby” group of ZIP codes see the price $10.
Error-Checking
A common use of conditionals is to check a function's input values for validity. If the inputs don't make sense, then the function should return some error-indicating value or print and error message.
For example, for our previous examples, we might want to check that
the zip
input is a valid ZIP Code. For the
celsius_to_fahrenheit(temp_celsius)
example from the last
class, we might want to check that the temp_celsius
input
is not below absolute zero.
A typical way to write such code would be as follows. It prints an error message and returns an error-indicating value when the input is invalid.
def celsius_to_fahrenheit(temp_celsius): """ Returns the temperature in degrees Fahrenheit equivalent to the given temperature in degrees Celsius """ if temp_celsius < -273.15: print "Invalid temperature: ", temp_celsius return None else: return temp_celsius * 9/5 + 32
The template for this would be as follows (the following is pseudo-code - put the appropriate values in!):
def fn(arguments): if arguments_are_invalid: print error_message return error_value else: Do_whatever_the_function_normally_does
Or, do you understand why the following would be equivalent?
def fn(arguments): if argument_are_invalid: print error_message return error_value Do_whatever_the_function_normally_does
For some functions, like the last exercises today, a more appropriate template would be
def fn(arguments): if valid_case_1: do_whatever_the_function_should_do elif valid_case_2: do_whatever_the_function_should_do … else: print error_message return error_value
-
You wrote the following function in last class. Modify it to add error-checking like this. What input values should be considered invalid?
rect_prism_area(width, height, depth)
College Cheers
A Rice tradition is to yell college cheers. However, it's annoying to have to memorize them, so here you'll write two functions to help you. Have fun comparing them with your neighbors.
These two examples share a number of similarities to what you'll be doing in the Rock-paper-scissors-lizard-Spock assignment code.
-
Write a function
your_college_cheer(college)
. For example, I would writehanszen_cheer(college)
. The function checks which college the input is, for example"Weiss"
, and it returns your favorite cheer for/against that college. Thus, the return value should be a string.If the
college
input isn't one of the Rice college names, then your function should just return an empty string. -
Write a function
your_college_cheers(college)
(Note the plural!). For example, I would writehanszen_cheers(college)
. It is like the previous function, but it instead chooses randomly among several cheers per college.