Grade Converter question


#1

Explain to me how this line of coding works?
" def grade_converter(grade):
if grade >= 90:
return “A”
elif grade >= 80:
return “B”
elif grade >= 70:
return “C”
elif grade >= 65:
return “D”
else:
return “F” "
What I was confused was the elif grade >= from 80 up until 65 grade, how does it lock in 89, 79, and 69?


#2

The statements are evaluated in order.
The cpu looks at the variable “grade” and compares it to the number 90
If grade is greater “A” is returned
If not the next statement is evaluated
The cpu compares grade to 80
If it is greater than 80 it returns “B”
If the number was higher than 89 the function would have already returned “A”

This process repeats until a a value gets returned.


#3

I see, Thanks for the information! Got my head spinning with this lesson…


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.