Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Python Functions

image text in transcribed

image text in transcribed

image text in transcribed

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("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)") Enter total points out of 600: Grade Calculator Grade Calculator # 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 def Get 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 by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Explain the strength of acid and alkali solutions with examples

Answered: 1 week ago

Question

Introduce and define metals and nonmetals and explain with examples

Answered: 1 week ago