From the course: Python Quick Start

Unlock this course with a free trial

Join today to access over 24,500 courses taught by industry experts.

If-elif and if-elif-else statements

If-elif and if-elif-else statements - Python Tutorial

From the course: Python Quick Start

If-elif and if-elif-else statements

An if-elif statement instructs a computer to perform a certain action, if a certain condition is met or a different action, if a different condition is met. For example, say you asked the user if it's raining and if they have an umbrella and based on the user's responses, a relevant message is displayed to the user. You could use an if-elif statement to do this. I can start by creating a variable named "raining" and set it to the user's response to the prompt, "Is it raining?" Yes or no? On this line, I can create another variable named "umbrella" and set it to the user's response to the prompt, "Do you have an umbrella?" Yes or no? And on this line, I'll begin to write an if-elif statement. I start with the keyword if followed by a compound condition since I want to check two things here. I want to check whether raining is yes and umbrella is yes. So right. raining == "yes" and umbrella == "yes" : If this compound condition is satisfied, I want to print out, "Don't forget your…

Contents