knerr cs21 notes...
back to schedule
WEEK03: booleans, logical operators, conditionals (if/else)
---------------------------------------------------------------
F: review some codes, write speeding ticket fine calculator
- grade program (output letter grade, given numeric):
"""
Return letter grades for numbers.
"""
def main():
g = input("please enter a numeric grade: ")
if g < 60:
letter = "F"
elif g < 70:
letter = "D"
elif g < 80:
letter = "C"
elif g < 90:
letter = "B"
else:
letter = "A"
print "a grade of %3d = %s" % (g, letter)
main()
- lettercase program (determine if letter is upper/lowercase)
"""
Determine case of each letter.
Author: J. Knerr
Date: 09Sep2008
"""
def main():
phrase = raw_input("phrase: ")
for ch in phrase:
if ch <= "z" and ch >= "a":
print "lowercase: ", ch
elif ch <= "Z" and ch >= "A":
print "uppercase: ", ch
else:
print "nonletter: ", ch
if __name__ == '__main__': main()
- don't write this one, but can you think of a way to do this?
(hint: another use of the % operator)
$ python isodd.py
please enter an integer: 5
...5 is O D D
$ python isodd.py
please enter an integer: 4
...4 is EVEN
- speeding ticket fine calculator:
Any speed clocked over the limit results in a fine of at least $50,
plus $5 for each mph over the limit, plus a penalty of $200 for any
speed over 90mph. Let's write a program that will take as input the
speed limit and the clocked speed. It will then print that the
clocked speed was under the limit or it will print the appropriate fine.
$ python speedlimit.py
I'll calculate the fine for a given speed and speed limit
driver's speed: 60
speed limit: 55
Fine = $75
- if you have time, can you figure out this one?
$ python drop_letter.py
phrase: i love computer science!!
new phrase: ilv optrsine!
$ python drop_letter.py
phrase: 0123456789
new phrase: 02468