Computer language is not English. Humans are great at guessing what subject is repeated in a compound sentence, computers are not. You always explicitly state what you are testing against on both sides of a logical operator.
That's because in the expression structure <left> and <right>
, both left
and right
are always independent expressions. Expressions can build on expressions on expressions, but a computer programming language will not just re-use (a part of) the left
expression in the right
expression.
So yes, you have to explicitly name grade
again.
Or you could use a different expression form. You could use a chained comparison expression; Python lets you collapse any expression of the form <foo> <comparison1> <shared> and <shared> <comparison2> <bar>
into <foo> <comparison1> <shared> <comparison2> <bar>
, and the shared
expression will be executed just once.
So if you turned
grade >= 80 and grade <= 89
into
80 <= grade and grade <= 89
you can replace that with
80 <= grade <= 89
However, note that the preceding test already handled the grade > 89
case, you can safely drop the upper bound tests:
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"
Last but not least, you can use a Computer Science trick. Rather than test each grade band separately, one by one, you could use bisection; this always works when your options are sorted.
Instead of starting at the highest value, start in the middle; that divides the possibilities in 2. From there, you keep halving the possibilities until you have the right grade band. This means you only have to do, at most, Log(N) tests for N possibilities, while starting at the top grade will require up to N tests. For 5 tests that's not much of a difference (1.6 steps on average, vs 5), but when N becomes really large, then you'll quickly notice a difference; if you had 1 million options, you could find the matching option in less than 14 steps, guaranteed.
The Python library includes an optimised implementation in the bisect
module:
import bisect
def grade_converter(grade):
grades = ['F', 'D', 'C', 'B', 'A']
limits = [65, 70, 80, 90]
return grades[bisect.bisect(limits, grade)]