Now You Code 2: Course Grade

In a previous homework you wrote a program to calculate the final letter grade in IST256 when given the total points out of 600.

The code appears below. Feel free to execute this code a few times in order to get a feel for how it works.


In [ ]:
print("IST256 Grade Calculator")
points = int(input("Enter total points out of 600: "))
if points >= 570:
    grade = "A"
elif points >= 540:
    grade = "A-"
elif points >= 510:
    grade = "B+"
elif points >= 480:
    grade = "B"
elif points >= 450:
    grade = "B-"
elif points >= 420:
    grade = "C+"
elif points >= 390:
    grade = "C"
elif points >= 360:
    grade = "C-"
elif points >= 300:
    grade = "D"
else:
    grade = "F"

print(f"Grade: {grade}")

Write - Refactor - Test - Rewrite Approach

The best way to get good at writing functions, a skill you will need to master to become a respectable programmer, is to use the Write - Refactor - Test - Rewrite approach. In this approch we:

  1. Write our code
  2. Refactor it into a function
  3. Test the function to make sure it works as expected
  4. Rewrite our code to call our new function

Step 1: Write code

The program has been written for you above. Your goal is to refactor it into a function, test the function then re-write the code.

Step 2: Refactor Into a function

One reason to re-factor code into a function is to simplify our code. It's easier to understand GetGrade() as opposed to all of those if ..elif statements which make up GetGrade itself.

Now re-factor the code into a user-defined Python function. The name of the function should be GetGrade(). The function should take a number of points as input and return the appropriate letter grade based on those points.

NOTE: There should not be any print() or input() statements in your function!


In [ ]:
## Step 2: Todo Write function defintion
## Function: GetGrade
## Arguments: points (eg. 540)
## Returns: grade (A, B+ etc...)

#TODO: Write function definition here

Step 3: Test our function

With the function complete, we need to test our function. The simplest way to do that is call the function with inputs we expect and verify the output. For example:

WHEN point='570' We EXPECT GetGrade(points) to return A 
WHEN points='540' We EXPECT GetGrade(points) to return A-

The first two are written for you but you will need to write the remaining tests to tests all the cases. As a general rule, there should be one test for each kind of output (in this case A through F). You only need to test at the boundaires. No need to test every single value between 0 and 600.


In [ ]:
# Step 3: Write tests. The first two tests were written for you. You must write the rest!

print("WHEN point='570' We EXPECT GetGrade(points) to return A ACTUAL:", GetGrade(570))
print("WHEN point='540' We EXPECT GetGrade(points) to return A- ACTUAL:", GetGrade(540))
#todo...

Step 4: rewrite the program to use the function

Finally re-write the original program, with a twist. Follow this algorithm

loop
    input a grade or type 'quit', save in variable text
    if text equals 'quit'
        break from the loop
    convert the text to an integer, store in the variable number
    call the GetGrade function with the number as input, store the output in letter grade
    print the letter grade

In [ ]:
## Step 4: Write program here from the algorithm above

Step 5: Questions

  1. Provide 3 advantages for programming a user function in your code like GetGrade

Answer:

  1. If we want to guarantee our GetGrade function is 100% accurate, what is the minimum number of test cases must we provide in step 3?

Answer:

  1. How many times is the GetGrade function called in 'Step 3: Test our function'? How many times was it called in step 3?

Answer:

Step 6: Reflection

Reflect upon your experience completing this assignment. This should be a personal narrative, in your own voice, and cite specifics relevant to the activity as to help the grader understand how you arrived at the code you submitted. Things to consider touching upon: Elaborate on the process itself. Did your original problem analysis work as designed? How many iterations did you go through before you arrived at the solution? Where did you struggle along the way and how did you overcome it? What did you learn from completing the assignment? What do you need to work on to get better? What was most valuable and least valuable about this exercise? Do you have any suggestions for improvements?

To make a good reflection, you should journal your thoughts, questions and comments while you complete the exercise.

Keep your response to between 100 and 250 words.

--== Write Your Reflection Below Here ==--


In [ ]:
# RUN THIS CODE CELL TO TURN IN YOUR WORK!
from ist256.submission import Submission
Submission().submit()