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 trialRana Olk
235 PointsWhy 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!
2 Answers
Rana Olk
235 PointsHello 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
19,870 PointsGood deal, now you know more than nothing.
To format your code check out the Markdown Cheatsheet below
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
Eric M
11,546 PointsGreat 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
235 Pointsdave 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!
Dave StSomeWhere
19,870 PointsDave StSomeWhere
19,870 PointsAre lines 4 through 7 indented within the if on line 2?