0

I'm trying to make this function return a string (the letter grade) instead of the float number but I've been stuck for hours. Yes they need to be floats. I know it seems a little messy right now I'm just trying to get it to work before I move onto cleaning it up.

def main():
    test_score_1 = float(input('Enter test score #1: '))
    test_score_2 = float(input('Enter test score #2: '))
    test_score_3 = float(input('Enter test score #3: '))
    test_score_4 = float(input('Enter test score #4: '))
    test_score_5 = float(input('Enter test score #5: '))

    print()
    print('Results:')
    determine_grade(test_score_1)
    determine_grade(test_score_2)
    determine_grade(test_score_3)
    determine_grade(test_score_4)
    determine_grade(test_score_5)

    print('Test #1:', test_score_1)
    print('Test #2:', test_score_2)
    print('Test #3:', test_score_3)
    print('Test #4:', test_score_4)
    print('Test #5:', test_score_5)


def determine_grade(score):
    if score >= 90.0:
        return 'A'
    elif score >= 80.0:
        return 'B'
    elif score >= 70.0:
        return 'C'
    elif score >= 60.0:
        return 'D'
    elif score >= 50.0:
        return 'F'


main()
3
  • 2
    You aren't doing anything with the return value when you call determine_grade(x) Commented Mar 15, 2022 at 9:16
  • I'm new to python, could you be more specific on what I have to do then? I had it working earlier but the problem I kept running into was that I couldn't have print statements in my function. Can you describe how I can return the strings to the main so I could display them? Commented Mar 15, 2022 at 9:19
  • You have to do something with the return value. You could, for example, assign it to the same variable used for the value you passed in, which seems to be what you assumed would happen, so test_score_1 = determine_grade(test_score_1). You could use other variables, or whatever you want. Commented Mar 15, 2022 at 9:32

2 Answers 2

2

you are not doing anything with your returned values from determine_grade function. Following should work

def main():
    test_score_1 = float(input('Enter test score #1: '))
    test_score_2 = float(input('Enter test score #2: '))
    test_score_3 = float(input('Enter test score #3: '))
    test_score_4 = float(input('Enter test score #4: '))
    test_score_5 = float(input('Enter test score #5: '))

    print()
    print('Results:')
    print('Test #1:', determine_grade(test_score_1))
    print('Test #2:', determine_grade(test_score_2))
    print('Test #3:', determine_grade(test_score_3))
    print('Test #4:', determine_grade(test_score_4))
    print('Test #5:', determine_grade(test_score_5))


def determine_grade(score):
    if score >= 90.0:
        return 'A'
    elif score >= 80.0:
        return 'B'
    elif score >= 70.0:
        return 'C'
    elif score >= 60.0:
        return 'D'
    elif score >= 50.0:
        return 'F'


main()

or if you wish to keep your code

def main():
    test_score_1 = float(input('Enter test score #1: '))
    test_score_2 = float(input('Enter test score #2: '))
    test_score_3 = float(input('Enter test score #3: '))
    test_score_4 = float(input('Enter test score #4: '))
    test_score_5 = float(input('Enter test score #5: '))

    print()
    print('Results:')
    # get value from function and save it in variable
    grade_1 = determine_grade(test_score_1)
    grade_2 = determine_grade(test_score_2)
    grade_3 = determine_grade(test_score_3)
    grade_4 = determine_grade(test_score_4)
    grade_5 = determine_grade(test_score_5)

    # display grades
    print('Test #1:', grade_1)
    print('Test #2:', grade_2)
    print('Test #3:', grade_3)
    print('Test #4:', grade_4)
    print('Test #5:', grade_5)


def determine_grade(score):
    if score >= 90.0:
        return 'A'
    elif score >= 80.0:
        return 'B'
    elif score >= 70.0:
        return 'C'
    elif score >= 60.0:
        return 'D'
    elif score >= 50.0:
        return 'F'


main()
Sign up to request clarification or add additional context in comments.

2 Comments

okay so they are getting changed, noted. How do I display them then? do I have to type a different variable? Im not sure what to be using to display them.
They are being displayed in the print statements?
0

You need to print the returning value of the function, not your original test scores:

def main():

    test_score_1 = float(input('Enter test score #1: '))

    print()
    print('Results:')
    x = determine_grade(test_score_1)

    print('Test #1:', x)

Or

def main():

    test_score_1 = float(input('Enter test score #1: '))

    print()
    print('Results:')
    print('Test #1:', determine_grade(test_score_1))

Your original code didn't work as you were calling the function determine_grade(test_score_1), which was returning the correct value, but it was discarded, as it wasn't used for anything. This can be solved by putting that right into the print statement (above), or assigning to a variable (first example).


Which leaves your new code at:

def determine_grade(score):

    if score >= 90.0:
        return 'A'
    elif score >= 80.0:
        return 'B'
    elif score >= 70.0:
        return 'C'
    elif score >= 60.0:
        return 'D'
    elif score >= 50.0:
        return 'F'
    
def main():

    test_score_1 = float(input('Enter test score #1: '))
    test_score_2 = float(input('Enter test score #2: '))
    test_score_3 = float(input('Enter test score #3: '))
    test_score_4 = float(input('Enter test score #4: '))
    test_score_5 = float(input('Enter test score #5: '))



    print()
    print('Results:')
    
    # Print test number, and the returned value of that test's grade
    print('Test #1:', determine_grade(test_score_1))
    print('Test #2:', determine_grade(test_score_2))
    print('Test #3:', determine_grade(test_score_3))
    print('Test #4:', determine_grade(test_score_4))
    print('Test #5:', determine_grade(test_score_5))

main()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.