Lecture 16 - Object Orientation and Python


Links


Python Exercises

Grade Conversion

In a file grades.py, create a function convert_to_letter_grade. The function takes a numeric grade from 0 to 100, and prints the associated letter grade using the following scale:

A
[90, 100]
B
[80, 90)
C
[70, 80)
D
[60, 70)
F
[0, 60)

Note that square brackets mean inclusive, and parenthesis means exclusive.

  • Batting Average

    Create a Python function called batting_average(appearances) in a file with the same name. The function parameter, appearances, is a list of ints that encodes the results of a baseball player's appearances at batting. Each plate appearance can be interpreted as follows:

    4Home Run
    3Triple
    2Double
    1Single
    0Walk
    -1Out

    The function should return the batting average for the plate appearances in the list. The batting average can be computed using the following equations:

    at_bats = plate_appearances - walks
    hits = singles + doubles + triples + home_runs
    batting_average = hits / at_bats

    >> print(batting_average([1, 1, -1]))
    0.6666666666
    >> pedroia_at_bats = [-1, 1, -1, -1, 1, -1, -1, 2, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 0, -1, 0, -1, -1, -1, 2, 1, -1, -1, -1, -1, 1, 0, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, 0, -1, 1, 1, -1, -1, 2, 0, -1, -1, -1, -1, -1, -1, -1, 1, -1, 2, -1, -1, -1, -1, -1, -1, -1, 1]
    >> print(batting_average(pedroia_at_bats))
    0.25396825396825395
    

  • Last modified: Tue Mar 18 09:26:24 EDT 2014