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 (Retired) Ins & Outs String Concatenation

Himanshu Singh
Himanshu Singh
1,224 Points

How to apply multiple 'if' statement?

How to apply multiple if statement? either us it like "if() if() else()" or like "if() else if() else()"

2 Answers

Himanshu Singh
Himanshu Singh
1,224 Points

We can use 'elif' command. For example.. name=input('What is your name? ') if(name=="Himanshu"): print(name+" is my brother") elif(name=="Nisha"): print(name+" is my sister") else: print("I don't know "+name)

After the first 'if' statement, write 'elif' on a new line and enter your information. 'elif' comes from a contraction of the words 'else' and 'if' and can be thought of a middle ground between just 'if' and 'else.'

Example:

name = input("What is your name? ")

if name == 'Rebecca':

print("Hello " + name)

elif name == 'Dean':

print(name + " is my brother's name.")

elif name == 'Ruth':

print("My mom's name is " + name + ".")

else:

print(name + " is not recognized.")