Hi! I just started learning Python 3 and i have a question about ‘Else If Statements’ exercise 11:
Calvin Coolidge’s Cool College has noticed that students prefer to get letter grades over GPA numbers. They want you to write a function called
grade_converterthat converts an inputted GPA into the appropriate letter grade. Your function should be named
grade_converter, take the input
gpa, and convert the following GPAs:
- 4.0 or higher should return
- 3.0 or higher should return
- 2.0 or higher should return
- 1.0 or higher should return
- 0.0 or higher should return
You should do this by creating a variable called
gradethat is first set to
Then, you should use
elifstatements to set
gradeto the appropriate letter grade for the
At the end of the function, return
The last part suggests a certain way of doing it but i started with another method that seemed to work aswell. Are both alright or should i use either one?
def grade_converter(gpa): if gpa>=4.0: return "A" elif gpa>=3.0: return "B" elif gpa>=2.0: return "C" elif gpa>=1.0: return "D" else: return "F"
def grade_converter(gpa): grade="F" if gpa >=4.0: grade= "A" elif gpa >=3.0: grade= "B" elif gpa >=2.0: grade= "C" elif gpa >=1.0: grade= "D" else: grade= "F" return grade
I thought maybe both are correct but one is preferred whenever we encounter more complex codes, so for future laziness, hope someone can enlighten me:) Thank you!