# conditionals.py # You need to understand boolean expressions to understand conditionals. # If you are not familiar with them, consult [DEM]. # absolute taking one argument (apparently of type integer) retuns the # absolute value of the given integer. To compute the absolute value, # it uses the conditional expression, i.e., if...else... Both syntax # and semantics of a conditinal expression is intuitive. # def absolute(x): if x < 0: return -x else: return x print absolute(-34) # A conditional expression can be of the cascaded if...elif... ...else... # form, again with intuitive meaning. def absolute(x): if x < 0: return -x elif x > 0: return x else: return 0 print "absolute(-34) = ", absolute(-34) print "absolute(34) = ", absolute(34) print "absolute(0) = ", absolute(0) # Exercise 1: # # Write a boolean function that determines if a given speed value is within # the legal freeway speed on I-10 outside of the city limits. Once you define # the function, write a piece of code that calls/uses/invokes/applies the # defined function and prints the computed result (true or false). # Exercise 2: # # Given a score out of 100, return a letter grade for the score. Let's assign # the letter grade by the following table: # # above 89: A # above 79 and below 90: B # above 69 and below 80: C # above 59 and below 70: D # below 60: F # # and call the function at least five times, each time producing a different # letter grade. # # It is okay to produce the final grade as a string value, e.g., "A" for an A. # Exercise 3: # # See Section 4.13 of [DEM] if you want to see more exercise problems.