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}")
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:
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.
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
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...
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
GetGrade
Answer:
GetGrade
function is 100% accurate, what is the minimum number of test cases must we provide in step 3?Answer:
GetGrade
function called in 'Step 3: Test our function'? How many times was it called in step 3?Answer:
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()