COMP 200: Elements of Computer Science
Spring 2013

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.

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

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.