Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics Types and Branching Comparisons

Blue DiamondXD
Blue DiamondXD
5,369 Points

im confuss about elif and if

help please

first_name = input("what is your first name?") #user input's there name
print("Hello,",first_name) 
if first_name == "Craig": #So if the user types "Craig" as there first name, then it's going to print out the code below.
    print(first_name,"is learning python")
elif first_name == "Nathan": # Otherwise or else if the user types "Nathan" then it prints the code below. 
    print(first_name,"is learning with fellow students in the community! Me too!")

If you want me to go in more detail let me know.

5 Answers

Finn Gray
Finn Gray
3,009 Points

Hi,

I'm fairly new but I hope this helps. if and elif( else if) essentially have the same use except that you use elif to add other if statements in the same section of your code. . Here's an example of them in use:

tomato = input("What color is a tomato?   )
if tomato == red:
    print("Tomatoes are red")
elif tomato == green:
    print("Tomatoes are only green when they're not ripe.")
elif tomato == yellow:
    print("Some varieties of tomatoes are in fact yellow."
elif tomato == black:
    print("Maybe you shouldn't eat that one.")
else:
    print("I'm out of ideas")

Probably not the best explanation but I hope this helps a little.

Jim Andrews
Jim Andrews
3,110 Points

If I remember correctly, these need an else statement at the end. Like you can have as many elif statements but the end needs an else in case they all fail. And the else obv has no parameters. So like you could write

else: print("who are you?")

This is a pretty good example

animal = input("What is your favorite kind of animal?   ")
if animal == "dogs": # checks if the animals = dogs
    print("Dogs are my favorite!!")
if animal == "cat": #checks if animal = cat
    print("I actually own 2 cats, and they are amazing")
else:  #(otherwise)If none of the of the other 'if statements' are true, then run this code below.
    print("{} are pretty cool as well!".format(animal))
Rana Olk
Rana Olk
235 Points

Hi Nathan, Im looking at above code, and it's exactly what I was confused about. Actually, When you have

if animal dogs AND If animal cats this program doesn't run right. Here is what you get:

1>what's your favorite animal? dogs
2>dogs are my favorite!
3>dogs are pretty cool as well!

It ends up adding the line (3) that it is ONLY supposed to appear if 'animal' is neither dogs nor cats. I don't understand why we are unable to add several "IF"s, followed by an 'else'.

It is only when we say If animal dogs ELIF animal cats

That we get the right result.

Rana Olk
Rana Olk
235 Points

Hello. I don't understand the following, using Nathan's code above:

If I write the following code:

1 animal=input("what's your favorite animal? ") 2 if animal=="dogs": 3 print("dogs are my favorite!") 4 if animal=="cats": 5 print("I own two cats, they're amazing") 6 else: 7 print("{} are cool.. I like them too!".format(animal))

I GET THIS: what's your favorite animal? dogs
dogs are my favorite!
dogs are cool.. I like them too!

The question is: Why did this code continue and put line 7 out as well? Im telling it to tell me one thing if the input is dogs. Something else if the input is cats. And something else if the input is anything other than dogs or cats.

And why does it only work if I put ELIF in line 4, rather than IF?

Thank you so much!

Guilherme Mergulhao
Guilherme Mergulhao
4,002 Points

Lets say you have 99 conditions you want (IF Statement) to add and 1 condition to happen if not a single IF statement worked (ELSE statement).

If you don't use elif, and the condition will fall on the 2nd condition, it will still check every single if condition after, even though the condition was already met.

But if you use elif, when the condition is met on 2nd check, it will skip all other 97 checks and go straight to the next part of the code and avoid any other conditions met.

So it's both better for performance and error avoiding.

Johannes Scribante
Johannes Scribante
19,175 Points

Hi Blue DiamondXD (interesting name btw)

Basically we want to structure our code to perform certain tasks, hence the name "control structure" for most conditional and looping logic.

The logic between the if and elif is best explain in words. Consider

basic example if the number is less then 80 print less than 80

a bit more if the number is less than 90 but more than 80 print the number is between 80 and 90 else if the number is less than 100 but more than 90 print the number is between 90 and 100

note to keep in mind we only want one of these conditions to occur not more than one. copy the code I have below and play with the variable a and see how the code responses. There is no right or wrong, it truly depends on the purpose of your code. They are sometimes just nicer to with.

# we want to be able to check for multiple conditions, 
# however this is not always as simple as one might think

a = 85

print('### single condition')

if a < 80:
    print('yay less than 80')

if a < 90:
    print('yay less than 90')

if a < 100:
    print('yay less than 100')

if a >= 100:
    print('oops more than or equal to 100')
# output
# > yay less than 90
# > yay less than 100

# but now we want to know if it falls in an exact bracket
print('### between cases')

if a < 80:
    print('yay less than 80')

if 80 <= a < 90:
    print('yay less than 90 but more than 80 with double condition')

if 90 <= a < 100:
    print('yay less than 100 but more than 90 with double condition')

if a >= 100:
    print('oops more than or equal to 100')
# output
# > yay less than 90

# but now we can we can prevent the double output with 
# an elif
print('### elif')

if a < 80:
    print('yay less than 80')
elif a < 90:
    print('yay less than 90 but more than 80')
elif a < 100:
    print('yay less than 100 but more than 90')
else:
    print('oops more than or equal to 100')

# output
# > yay less than 90 but more than 80