Watch the video and take notes, pay attention to how if statements can be extended (one or the other approach).
Run and walk through the example (in code window).
Read the task and write code to solve.
Make a note of successful code in your book.
Answer questions in your book.
Watch the video and create notes in order to be able able to apply the knowledge given.
# Example code (if else statement SELECTION)
hour = int(input("Enter the hour (24 hour clock) :")) # Allows a user to input hour of the day
if hour < 18: # if condition is the hour entered less than 18 (6o clock)
print("Good day") # if True then print output "Good day" on the screen
else: # if False and hour is anything ELSE
print("Good evening") # output "Good Evening"
# REMEMBER else is part of the if statement but needs no conditional statement as it will deal with everything ELSE
Apply the your knowledge of if else statements and write a program that will allow a user to enter a number, if that number is greater than 100 the program will output "The number entered is greater than 100" if it is not then the program will output "The number entered is less than 100"
User input number
if number inputted is greater than 100, output "The number entered is greater than 100"
if the number is not greater than 100, output "The number entered is less than 100"
number = int(input("Enter a number :"))
if number > 100:
print("The number entered is greater than 100")
else:
print("The number entered is less than 100")
Identify and describe what will occur in the code exercise if the user entered 100.
Describe why the else statement needs no conditional statement.
Describe how an if statement alters the path of a programs execution.
Identify and describe what will occur in the code exercise if the user entered 100.
When the user inputs 100, the if statement will look at the conditional statement and 100 is not greater than 100 so it will trigger the else statement and output the statement "The number is less than 100"
Describe why the else statement needs no conditional statement.
The else part of the if statement doesn't need a conditional statement/expression as it works when the initial if statement is False, thus accepting everything "else".
Describe how an if else statement alters the path of a programs execution.
This approach always gives two definite paths to a program. If it True execute/run the code indented under the if statement, if False execute/run the code indented under the else.