[Tutor] help with elif statements

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Oct 13 04:04:31 CEST 2005



On Wed, 12 Oct 2005 andrade1 at umbc.edu wrote:

> below is my code and everytime I input a value of 16 or more it keeps
> returning sophomore. could anyone help me figure out what to change so
> that it won't return sophmore for things greater than or equal to 16?

If we have the following:

### Pseudocode
if A:   [bodyA]
elif B: [bodyB]
elif C: [bodyC]
else:   [bodyD]
###############

only of those bodies will be entered, regardless if more than one
condition is true.  That is, we'll enter the body of the first condition
that ends up true, and ignore the rest.  We call this an "exclusive"
branching point for this reason.

If it helps, the above pseudocode could also be rewritten as the following
(although it would not be idiomatic):

if A:
    [bodyA]
else:
    if B:
        [bodyB]
    else:
        if C:
            [bodyC]
        else:
            [bodyD]



More information about the Tutor mailing list