Question

What you're creating Using a case statement, create a function that accepts a letter grade and...

What you're creating

Using a case statement, create a function that accepts a letter grade and returns a minimum score to achieve that grade.

How you'll do it

You learned sequence structure, statements executed in order, then selection statements with if statements, then looping statements. In some languages, there is a case selection statements, sometimes called switch/case.

In Python, you can write your own case statement using a function and a dictionary.

In this assignment you will write a function switch_average that accepts a letter grade { A, B, C, D, F } and returns the correcting numeric average { 90, 80, 70, 60, 50 }, respectively.

In Module8 project

  • Create a file assign_average.py with the unimplemented switch_average() function.
  • Create a file test_assign_average.py
    • Write a function to test A (and a) return value
    • Write functions for the other 4 keys
    • Write function to test if a non-key is passed
      • What do you expect to happen? Should an exception be raised in the switch? How should you handle the case of no key found?
    • Run the tests and see them fail
    • Begin the implementation of switch_average.
      1. Make one test function pass for key value of 'A.
      2. Make the next test function pass for the next value.
      3. Repeat above step for each test until all are passing.

Notes/Hints

  • This assignment can be achieved using a structure similar to the first case example given in class
  • You must complete this using case statements even though it could be done using if/elif statements
  • Your tests should cover all grades so there should be at least 5 of them
0 0
Add a comment Improve this question Transcribed image text
Answer #1

def switch_average(inp):
grades_dict = {'A':90, 'B':80, 'C':70, 'D':60, 'F':50 }
inp = inp.upper()
res = grades_dict.get(inp, 'Invalid Grade')
#get function for a dictionary will return value of key provided
# if the key is not found it will return the second argument in the fumction.
return res

print(switch_average('A'))
print(switch_average('a'))
print(switch_average('B'))
print(switch_average('b'))
print(switch_average('C'))
print(switch_average('c'))
print(switch_average('D'))
print(switch_average('d'))
print(switch_average('F'))
print(switch_average('f'))
print(switch_average('X'))
print(switch_average('x'))

Add a comment
Know the answer?
Add Answer to:
What you're creating Using a case statement, create a function that accepts a letter grade and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Python programming, defines a function that accepts a letter grade as a parameter and returns...

    Using Python programming, defines a function that accepts a letter grade as a parameter and returns the GPA. If the grade is not one of the standard letter grades (A, B, C, D, F), return -1 to indicate this.

  • C++ //get_letter_grade.h /* Write a function gpa_to_letter_grade that returns a string and accepts a double gpa...

    C++ //get_letter_grade.h /* Write a function gpa_to_letter_grade that returns a string and accepts a double gpa parameter */ //get_letter_grade.cpp /* Write function code for gpa_to_letter_grade that returns a string and accepts a double gpa parameter YOU MUST USE A SWITCH STATEMENT Given a double 3.5 returns the string A TIP: You'll have to convert the double to an int using multiplication Table 3.5 to 4 returns an A 3.0 to 3.49 returns a B 1.7 to 2.99 returns a C...

  • Question 2 Use a switch statement to write the following program: Using C++ The program prompts...

    Question 2 Use a switch statement to write the following program: Using C++ The program prompts the user for a letter grade (of type char). The list of valid letter grades is: A B C D E F The program should consider both lower and upper case The program will then display the following messages: For grade ‘A’: display “Excellent” For grade ‘B’: display “Good” For Grade ‘C’: display “Average” For grade ‘D’ or ‘E’: display “Below Average” For Grade...

  • Need help with the following: [i'm using SQL Server 2008] Create a function that accepts a...

    Need help with the following: [i'm using SQL Server 2008] Create a function that accepts a zipcode string (5 characters) as input and returns 1 if thw zipcode is in the zipcode table and 0 if it is not . also create a test file that demonstrates using the function.

  • You will create a Grade Program that will calculate students’ weighted averages. You are going to...

    You will create a Grade Program that will calculate students’ weighted averages. You are going to have the user enter lab grades, test grades, and project grades. You will then display a grade report of each individual average along with their overall average and letter grade. Ask the user to enter their lab grades. Call the averageGrade function that will allow the user to enter their grades and calculate their average. averageGrade Function: This will pass the average back to...

  • 8. Exercise 5.6 Switch Statements Objective: Create a switch statement that accepts an integer input from...

    8. Exercise 5.6 Switch Statements Objective: Create a switch statement that accepts an integer input from 1 to 12 and returns a String of the related month name. For any other number, return Invalid Number. Steps: Create a file named Ex_5_6.java and appropriate class name and main() method. Create a method using the following signature: pubilc static String monthNumberToString(int monthNumber) In the main() method, add in the following code: System.out.println(monthNumberToString(1)); This will allow you to test your code. Modify the...

  • 1. Create a program that takes a numerical score and outputs a letter grade. 2. In...

    1. Create a program that takes a numerical score and outputs a letter grade. 2. In this program, create two void functions titled makeScore and theGrade with an int argument. 3. The function makeScore should have a Reference parameter and theGrade should have a Value parameter. Note: Specific numerical scores and letter grades are listed below: 90-100 = Grade A 80-89 = Grade B 70-79 = Grade C 60-69 = Grade D 0-59 = Grade F 4. The function makeScore...

  • MATLAB Code: ?MATLAB Code: Write a function that converts a given letter grade it will print...

    MATLAB Code: ?MATLAB Code: Write a function that converts a given letter grade it will print its equivalence in numerical scale. For example if the user enters A, then your function should print a message stating that the grade must be between 90 and 100. B is between 80 and 89, etc. Everything under 60 is F. Use the switch-case function. Use fprintf to display the results on the screen.

  • Design a function named max that accepts two integer values as arguments and returns the value...

    Design a function named max that accepts two integer values as arguments and returns the value that is the greater of the two. For example, if 7 and 12 are passed as arguments to the function, the function should return 12. Use the function in a program that prompts the user to enter two integer values. The program should display the value that is the greater of the two. Need Variable lists, Psuedocode, ipo chart, Flow Chart, and a python...

  • Homework 4 – Create your own Function Create your own function in C that accepts one...

    Homework 4 – Create your own Function Create your own function in C that accepts one input number and returns a double number. The themes for the functions should be one of the following:  Calculates 3.14 times of the square of input number. For example, if 2 is input then 12.56 should be returned. (e.g. 3.14*2*2 = 12.56)  Divides the number by 3 and returns the result. For example, if 6 was input then 2.0 should be returned....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT