[Tutor] (no subject)
Danny Yoo
dyoo at hashcollision.org
Thu May 15 06:15:47 CEST 2014
> if curraverage>= 90:
> grade= "A"
> lettergrades.append(grade)
> else:
> if curraverage >= 80 and curraverage < 90:
> grade= "B"
> lettergrades.append(grade)
> else:
> if curraverage >= 70 and curraverage < 80:
> grade= "C"
> lettergrades.append(grade)
> else:
> if curraverage < 70:
> grade= "F"
> lettergrades.append(grade)
Just wanted to note that this style of cascading if statements is a
little unusual here. Since you know that only one of the tests is
going to be true, you can use a more parallel structure.
That is, if you're doing something like:
##################
if test1:
...
else:
if test2:
...
else:
if test3:
...
else:
...
##################
and if you know that test1, test2, and test3 don't overlap, you can
express this as:
##################
if test1:
...
elif test2:
...
elif test3:
...
else:
...
##################
and avoid the Tower of Pisa-style code.
More information about the Tutor
mailing list