Quiz 11(b) Thu 8 Nov 2007

  1. Give Python code for the function InString with two arguments: a string s and a character c. InString returns True if c occurs in s and False otherwise. Use the assert statement to formalize the preconditions for this function.

    Answer: the function InString.py .

  2. The function math.ln() will raise an exception when its argument is negative or 0. Give the python code for a function MyLn(x) that will return the math.ln of x if x is positive. For zero and negative values, an error message must be displayed. You must use exceptions and distinguish between 0 and negative values.

    Answer: MyLn.py and main test program.