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 trialKeifer Joedicker
5,869 PointsIs 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
Front End Web Development Techdegree Graduate 33,271 Pointsit 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.
Iain Simmons
Treehouse Moderator 32,305 PointsIt 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
Keifer Joedicker
5,869 PointsKeifer Joedicker
5,869 PointsThanks James!