Recursion will come up later in the course so don’t divert your attention, just now. This way you will recognize the error and be able to apply it to your current circumstance. Python is very dependent upon indentation clues to distinguish scope. Don’t depend on the editor. Do your own deliberate indentations to represent the scope you intend. (Coding tip)
So, in this example, you are asked to create a variable called grade, in the solution that codecademy gives me it assigns “F” to the grade variable. Is there a reason why he assigns this letter specifically, or can it be left blank somehow so we then call the variable we desire later on?
keep on getting error saying " It looks like your function didn’t convert the grades correctly. 4.1 should return the letter grade A, but instead it returned False."
When running the code, the output is technically correct as it prints out the correct grade letter depending on the input value (“A” in this example) but it also prints out the actual GPA value from my argument (here 4.1), as below
A
4.1
I also gets this message when running the code: It looks like your function didn’t convert the grades correctly. 4.1 should return the letter grade A, but instead it returned 4.1.
Could someone explain what I’m doing wrong ? The solution starts by defining the variable grade as “F”, which I don’t understand…
in the example question with donations that goes along with the elif exercise it states that if the elif statements were just if, then it would print all the possible return statements, which it does
But why when I change all of the elif statements in the code above, it still returns just one answer?
there is a difference between print and assigning to a variable. In the latter case, the variable just gets overwritten every time, still resulting in a single value. print will print each time
the ultimate test would be to combine the exercise with the example question (have assignment and print at each if clause.
grade = 86
if grade >= 90:
print("A")
elif grade >= 80 and grade < 90:
print("B")
elif grade >= 70 and grade < 80:
print("C")
elif grade >= 60 and grade < 70:
print("D")
else:
print("F")
If you do not include the “and” statement and the order of checks is different from “top to bottom”, would it not result in printing multiple responses? You need the “and” statements to include an upper bound on the grade classification or else a grade of 86 would meet multiple criteria (i.e. 86 is greater than 80 as well as 70, 60, etc), right?
Edit: I just checked it by changing the order of “if” checks without the “and” statement, and the “correct” code will produce an incorrect answer.
Top down or bottom up won’t matter so long as the sequence is maintained. We cannot get to the fourth floor without first going to the lower three floors, in order. Nor can we get to the first floor without going through the floors above it.
There is no required extra logic so long as the sequence order is ascending or descending.
if x < 60: # F
if x < 70: # D
if x < 80: # C
if x < 90: # B
# A
The user isn’t writing the code, we are. Code is written using the best reasoning of the programmer. So long as the relational operators are correctly chosen, and there are no assumptions, the language promises to interpret our code the way we write it.