In: Computer Science
Enter a Letter grade: B-
The numeric value is 2.7
Test Cases:
B- (should print 2.7)
A+ (should print 4.0)
B+ (should print 3.3)
C (should print 2.0)
F+ (should print 0.0)
G (should print “no such grade”)
Python, keep it simple
Hi,
Hope you are doing fine. I have coded the above question in python. Since you have mentioned to keep it simpke, I have tried to make it as easy as possible. The logic of the code is pretty straighforward and the code is explained through comments that have been highlighted in bold. Also please have a look at the code snippet and out put to get a better understanding about the indentation and execution of the program.
Program:
#taking input from user with a prompt to enter the
letter grade and storing it in grade
grade=input("Enter a Letter grade: ")
#conversion is a dictionary that store all the possible
coversion of the grades in the form of key value pairs. It has all
the grades given in the question
#Value of each grade is a floating point number that are as per the
conditions mentioned in the question
conversion={'A':4.0,'A+':4.0,'A-':3.7,
'B':3.0,'B+':3.3,'B-':2.7,
'C':2.0,'C+':2.3,'C-':1.7,
'D':1.0,'D+':1.3,'D-':0.7,
'F':0.0,'F+':0.0,'F-':0.0}
#if grade entered by the user is not present in the
dictionary as a key then
if grade not in conversion.keys():
#we print that there is no such grade
print("no such grade")
#else if the grade entered is present in the dictionary
then
else:
#the value at that key is printed which would be its
numeric conversion.
print("The numeric value is ",conversion[grade])
Executable code snippet from jupyter notebook:
Sample outputs of given test cases: