The code doesn’t refresh when I start ‘15. The Big if’. Instead, the correct code from ‘14.I Got 99 Problems, But a Switch Ain’t One’ remains in script.py. In an effort to work around this I manually wrote the correct code. However, this error message keeps appearing… can anyone help?
I see this was posted a little while ago. Just in case you or anyone else has/is run into this issue:
You do not need to put a bool after the else in line 16. Your program is unable to understand your expression after the : . All other statements look correct. So your program will already be able to deduce that anything below 65 will register as a F.
Please let me know if I explained this correctly. It sounds right in my head.
I also felt the need to add the “and grade < 90”, “and grade < 80”, etc., because I thought
a grade of “75” might return a grade of C and D and F. But, it turns out that wasn’t necessary. Can someone explain why? Is Python just reading top-down, so once it assigns a value it doesn’t continue down the line to see what else might apply to it? I hope that makes sense.
If .. elif .. else statements are always top, down and only one branch can ever be followed (the one in which the condition is satisfied). If none of the branches are followed in the absence of an else clause, then nothing is returned from the function, otherwise the else clause is the default branch.
I found it simpler to start with the lowest value and gradually work up to the highest.
if x < 60: y = 'F'
elif x < 70: y = 'D'
elif x < 80: y = 'C'
elif x < 90: y = 'B'
else: y = 'A'
Can someone explain why my code did not work? I now understand that the if else statements go from top to down order so using and statements like I did are not necessary, but I dont understand why using and like I did should still not work? Why is it not printing the correct results?
# Complete the if and elif statements!
if grade >= 90:
elif grade > 79 and grade < 90:
elif grade > 69 and grade > 80:
elif grade > 64 and grade < 70:
# This should print an "A"
# This should print a "C"
# This should print an "F"
As a consideration, something less than 60 is just that.
if x < 60:
We have ruled out the failure off the top. And, we know that the mark will pass, after this point in the logic. It’s really just climbing a ladder…
if x < 60:
if x < 70:
if x < 80:
if x < 90:
if x < 100:
if x == 100:
>>> get_letter_grade(101) # Reaching for the rung that is not there
Traceback (most recent call last):
File "<pyshell#25>", line 1, in <module>
File "<pyshell#24>", line 14, in get_letter_grade
As crude as this may appear, it is tolerant of floats, which is something that so many examples posted in these forums would fail at. If we’re meant to have intervals, then make sure there are no gaps between them.