[Tutor] Python help

Steven D'Aprano steve at pearwood.info
Mon Apr 2 20:19:38 EDT 2018


On Tue, Apr 03, 2018 at 01:00:59AM +0100, Alan Gauld via Tutor wrote:

> You need to use 'in' instead. That will check whether
> or not Grade is *one* of the values in the tuple.
> 
>     if Grade not in 'A','B','C','D','E','F':

Actually, that returns a tuple consisting of a flag plus five more 
strings:

py> 'A' in 'A','B','C', 'D', 'E', 'F'
(True, 'B','C', 'D', 'E', 'F')

We need to put round brackets (parentheses) around the scores:

    if Grade not in ('A','B','C','D','E','F'):


Don't be tempted to take a short-cut:

    # WRONG!
    if Grade not in 'ABCDEF':

as that would accepted grades like "CDEF" and "BC".



-- 
Steve


More information about the Tutor mailing list