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

Rana Olk
Rana Olk
235 Points

Why doesn't adding to "IF"s work in this program?

Hello. I don't understand the following:

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!

Dave StSomeWhere
Dave StSomeWhere
19,822 Points

Are lines 4 through 7 indented within the if on line 2?

2 Answers

Rana Olk
Rana Olk
235 Points

Hello David, You know, I think I figured it out. (Yeah, indentations are there...for some reason the formatting didn't type correctly in my question)

The issue is that code is being read line by line, hence, when we get to line 4 >> If animal=="cats" the computer is once again comparing 'dogs', and stating that this condition is 'false', and hence, moving directly to line 6, which says else: ( which it pays attention to because animal is NOT cat). It continues to 7) which tells it what to do, which is to print "dogs are cool..I like them too"

Im literally on day 2 of this course, and know nothing about programming. So I may not be using the correct terminology here...But I totally get it now. Pretty cool.

Thank you for responding :)

Dave StSomeWhere
Dave StSomeWhere
19,822 Points

Good deal, now you know more than nothing. :smile:

To format your code check out the Markdown Cheatsheet below :arrow_heading_down:

Then you get a nice code block like below:

animal = input("what's your favorite animal? ")

if animal == "dogs":
    print("dogs are my favorite!")  # print only for dogs
elif animal == "cats":
     print("I own two cats, they're amazing")  # not dogs and only cats
else:
    print("{} are cool.. I like them too!".format(animal))  # anything not dogs or cats

Just ask if you need assistance :tropical_drink:

Eric M
Eric M
11,507 Points

Great work figuring out the logic for yourself. I'm going to mark this as best answer so that this question topic looks "done" :)

And Dave thanks for pointing out the markdown cheatsheet. Better formatted questions tend to get answered quicker.

Rana Olk
Rana Olk
235 Points

dave thanks so much!! I just figured out what the markdown cheatsheet was and what its for. I was wondering why I couldn't get that black box:) Im having so much fun!