Watch the video and take notes, pay attention to how if statements can be extended (group 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 elif else statement SELECTION)
hour = int(input("Enter the hour (24 hour clock) :")) # Allows a user to input hour of the day
if hour < 12:
print ("Good morning")
elif hour < 15:
print("Good afternoon")
elif hour < 18:
print("Good evening")
else: # if False and hour is anything ELSE
print("Good night") # output "Good night"
# REMEMBER:
# if always starts a if elif else statement and the remainder are elif or finally a else
# if elif else are look upon as a group and only one set of indented instructions will execute/run
# They are below the first conditional statement that is True. The rest will be ignored
# else is part of the if statement but needs no conditional statement as it will deal with everything ELSE
Apply your knowledge of if elif and else statements to write a program that will apply tax based on how much an individual has earnt.
A user will input their weekly wage and the computer will apply 50% tax if the wage is greater than or equal to £100, 25% tax if the wage is greater than or equal to £75, 15% tax if the wage is greater than £50 any other amount below £50 will else will be taxed at 10%. Finally output the final taxed wage on the screen for the user.
Input weekly wage from user
if wage is greater than or equal to 100 then deduct 50% of the value from wage
else if wage is greater than or equal to 75 then deduct 25% of the value from wage
else if wage is greater than or equal to 50 then deduct 20% of the value from wage
else wage less than 50 then deduct 10% of the value from wage
output the final taxed wage on the screen
Help: ( wage / 100 )* percentage amount will give you the amount to remove from the wage
wage = int(input("Enter your weekly wage in pounds :"))
if wage >= 100:
wage = wage - (wage/100*50)
elif wage >=75:
wage = wage - (wage/100*25)
elif wage >=50:
wage = wage - (wage/100*15)
else:
wage = wage - (wage/100*10)
print("After tax wage is £",wage)
Describe why if elif else are described as selection
Define a rule that would describe the way if elif else statements are executed by a computer
Explain using the code example below as an example, how the output on the screen would change when using just a series of if statements instead of if elif.
hour = int(input("Enter the hour (24 hour clock) :"))
if hour < 12:
print ("Good morning")
if hour < 15:
print("Good afternoon")
if hour < 18:
print("Good evening")
else:
print("Good night")
Describe why if elif else are described as selection
The computer selects which path / indented instructions to execute after evaluating the conditional statements. One path will be selected based on the conditional statement being True.
Define a rule that would describe the way if elif else statements are executed by a computer
Looks at all the paths as a group and will only have the ability to select one, always starts with if and a conditional statement, subsiquent paths will use elif and a conditional statement, can end with a else that is a catch all. The computer will only ever check the conditional statements from top to bottom, the first one that is True it follows that path and executes the indented code, the remainder it ignores and passes over.
Explain using the code example below as an example, how the output on the screen would change when using just a series of if statements instead of if elif.
If you just used a series of if statements one after the other, the computer could trigger more than one set of indented instructions selecting several paths instead of selecting only one from the elif group statement. So for example if you input the number 2, it would trigger the first set of indented code as 2 < 10 True. It would then trigger the following indented code as 2 < 15 True. It would then trigger the final if statement as 2 < 18 True.
The screen output would be:
Good morning
Good afternoon
Good evening
Good night
Using a series of if statements would trigger any True conditional statement and the associated code whereas using an if elif group statement would only trigger the first True conditional statement ignoring the remainder.