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()
CodePudding user response:
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()
CodePudding user response:
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()