Download presentation
Presentation is loading. Please wait.
1
If, else, elif
2
Using the if statement Branching is a fundamental part of computer programming. It basically means making a decision to take one path or another. Through the if statement, your programs can branch to a section of code or just skip it, all based on how you’ve set things up
3
Introducing the Password program
Open the file password.py in the Chapter 3 folder. Right click, and select Edit with IDLE 3.5 The password program uses the if statement to simulate the login procedure of a highly secure computer system. The program grants the user access if he or she enters the correct password.
4
Examining the if statement
The key to the program is the if statement The if statement is pretty straightforward. You can probably figure out what’s happening just by reading the code. If the password is equal to “secret”, then “Access Granted” is printed and the program continues to the next statement. But, if it is not equal to secret, the program does not print the message and continues directly to the next statement following the if statement.
5
Conditional statement syntax
If is a conditional statement that executes some specified code after checking if its expression is True Here is an example of if syntax statement: In this example, 8 < 9 is the checked expression and print (“Eight is less than nine!” is the specified code.
6
Using indentation to create blocks
Notice in the second line of the in statement, print (“Eight is less than 9”), is indented. By indenting this line, it becomes a block. A block is one or more consecutive lines indented by the same amount. Indenting sets lines off not only visually, but logically too. Together they form a since unit. Blocks can be used as part of an if statement. They’re the statement or group of statements that gets executed if the condition is True. In the password program, the block is the single statement print(“Access Granted.”)
7
Conditional Statement Syntax
Do you think the print statement will print to the console in the following code? What will happen when you change today to “November 8”
8
If statements Looking at the example below, in the event that some_function() returns True, then the indented block of code after it will be executed. In the event that it returns False, then the indented blocked will be skipped *Also, make sure you notice the colons at the end of the if statement. They are important
9
Practice: In the figure below, there are two functions. Don’t worry about anything unfamiliar. We will be going over that soon. Replace the first underline with an expression that returns True Replace the second underline with an expression that returns True Run the code What does the code print?
10
The else statement The else statement complements the if statement. An if/else pair says: “If this expression is true, run this indented code block; otherwise run this code after the else statement.” Unlike if, else doesn’t depend on an expression. For example:
11
Introducing the granted or denied program
The Password program did not do anything if the wrong password was entered. Open Program Granted or Denied in the Chapter 3 Folder This program solves this problem by using the else clause
12
Practice Complete the else statement below and run the code. Note the indentation for each line. What did the code do?
13
The Elif statement “Elif” is short for “else/if.” It means exactly what it sounds like: “otherwise, if the following expression is true, do this!” In the example below, the elif statement is only checked if the original if statement is False
14
Practice In the first blank, fill in the if statement to check if answer is greater than 5. In the second blank, fill in the elif so that the function outputs -1 if answer is less than 5
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.