user14173601
user14173601

Reputation:

python function code does not meet requirments

I have an assignment for my class and I do not know what's wrong with it. Here is the assigment:

Write a program that prompts the user to input a letter grade in either capital or lowercase. Your program should then output the correct number of the GPA. See the table below for possible inputs and expected outputs. Your program should define at least one function named GPAcalc that is called from your main program.

The GPAcalc function should take exactly one parameter—a string for the letter grade as lower or upper case. It will return either an integer for the GPA value or a string Invalid, if the letter grade parameter is invalid.

Here is my code:

grade = input('Enter your letter grade: ')
def GPAcalc(grade):
    if grade == 'a'or grade == 'A':
        print('Your GPA score is: 4')
    elif grade == 'b'or grade == 'B':
        print('Your GPA score is: 3')
    elif grade == 'c'or grade == 'C':
        print('Your GPA score is: 2')
    elif grade == 'D'or grade == 'd':
        print('Your GPA score is: 1')
    elif grade == 'f'or grade == 'F':
        print('Your GPA score is: 0')
    else:
        print('Invalid')
GPAcalc(grade)

Here is the criteria that has failed:

This test case checks that your program is properly using the GPAcalc function to determine output.

This test case runs the GPAcalc function directly and checks the returned value.

This test case runs function directly and checks the returned value.

Upvotes: 0

Views: 2397

Answers (2)

user2390182
user2390182

Reputation: 73450

It will return either an integer for the GPA value or a string Invalid

This your function does not! You can do something like:

gpa = dict(zip("abcdf", (4, 3, 2, 1, 0)))

def GPAcalc(grade):    
    return gpa.get(grade.lower(), "Invalid")

grade = input('Enter your letter grade: ')
print("Your GPA score is: {}".format(GPAcalc(grade)))

Upvotes: 1

Shadowcoder
Shadowcoder

Reputation: 972

grade = input('Enter your letter grade: ')
def GPAcalc(grade):
    if grade == 'a'or grade == 'A':
        return 4
    elif grade == 'b'or grade == 'B':
        return 3
    elif grade == 'c'or grade == 'C':
        return 2
    elif grade == 'D'or grade == 'd':
        return 1
    elif grade == 'f'or grade == 'F':
        return 0
    else:
        return 'Invalid'

Upvotes: 0

Related Questions