Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (2015) Logic in Python If This Then That

Keifer Joedicker
Keifer Joedicker
5,869 Points

Is there a proper method of assigning conditions to the if or elif functions?

Say I have multiple conditions, how should I determine whether or not a condition is the first if or second elif?

2 Answers

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

it doesn't really matter, you just need to cover all possibilities of whatever you're doing. after an if and a series of 0 or more elifs, you have the else as a default, if no other condition is met.

It only matters if some conditions have a higher priority or if there is some sort of overlap between the conditions.

e.g.

def legal_to_drink(age, country):
    if age >= 21:
        legal = True
    elif age >= 18 and country != 'USA':
        legal = True
    else
        legal = False

    return legal

legal_to_drink(32, 'Australia')  # returns True
legal_to_drink(25, 'USA')  # returns True
legal_to_drink(20, 'USA')  # returns False
legal_to_drink(20, 'UK')  # returns True
legal_to_drink(16, 'UK')  # returns False