Introduction to Python Programming Part 4 IF Statements

  • Slides: 38
Download presentation
Introduction to Python Programming Part 4: IF Statements and Boolean

Introduction to Python Programming Part 4: IF Statements and Boolean

How to use this resource Skill Explanations: Pink Slides • Within each unit, you

How to use this resource Skill Explanations: Pink Slides • Within each unit, you will learn how to develop and apply a range of Python programming skills. Skill explanations are in pink. Tasks: Rookie Pro Beast • After reading the explanation of a skill, apply it within the given tasks. The tasks are categorised into Rookie (Easy), Pro (Medium) or Beast (Hard). Challenges: Rookie Pro Beast • Once you have learned how to apply your new skills, demonstrate your ability by completing the given challenges. Don’t forget the debugging task! • Once complete, review your performance!

Learning Objectives Rookie • To understand why decisions (IF statements) are used within programming.

Learning Objectives Rookie • To understand why decisions (IF statements) are used within programming. • To understand how use Boolean operation within an IF statement (<>==). Pro • To accurately write IF statements using a range of Boolean operations. • To create alternative code outcome using IF and ELSE programming techniques (the python else function). Beast • To use the Operators AND & OR within IF statements. • To add multiple decisions using the ELSE IF programming technique (the python elif function).

Skill Contents Once you complete a skill, colour code the box to show your

Skill Contents Once you complete a skill, colour code the box to show your level of confidence. You can always revisit the skill later on to boost your confidence. Key I am very good at this skill and could show a friend how to do it. I am confident I can perform this skill on my own. I need to improve my understanding of this skill. Colour Code Contents Understanding Boolean > < = != IF statements IF-ELSE statements ELSE-IF statements Advanced conditions AND OR Nested IF statements Confidence Level

Boolean Logic and IF Statements • An IF Statement, or conditional statement, allows different

Boolean Logic and IF Statements • An IF Statement, or conditional statement, allows different actions to be performed dependant on the outcome. • An IF statement only generates one of the following outcomes: TRUE or FALSE. • In order to check for a TRUE or FALSE outcome, you need to use some Boolean logic operators. The most common ones are explained below: Python operator Explanation Example explanation == > < != >= Equal to IF A == B: Checks if A is equal to B Greater than IF A > B: Checks if A is greater than B Less than IF A > B: Checks if A is less than B Not equal to IF A != B: Checks if A is not equal to B Greater than or equal to IF A >= B: Checks if A is greater than or equal to B <= Less than or equal to IF A <= B: Checks if A is less than or equal to B

Task 1: Understanding Python Boolean State if the outcome of the Boolean checks below

Task 1: Understanding Python Boolean State if the outcome of the Boolean checks below are either True or False when: X = 10 Y=5 Boolean X<Y X == Y Y <= X X >= Y Y>X X != Y True or False?

Skill 1: IF Statements • An IF statement allows for an action to be

Skill 1: IF Statements • An IF statement allows for an action to be performed only when the necessary condition (TRUE) is met. • For example, IF your alarm clock is going off, get up! Flowchart Pseudo code START IF the Decision = TRUE THEN: Perform Action 1 ELSE: END IF END

How to write an IF statement in Python This is an example of an

How to write an IF statement in Python This is an example of an IF statement. We can break it down as follows: Explanation Code If your condition is checking the value of a variable, declare this first: Write your condition using the required Boolean symbol(s): If the condition is TRUE, the indented action below is run. This action is indented to show it is dependant on the code above: Note: the code below the condition is indented. This means it will only run if the condition above is true.

Task 2: Rookie 1. Ask the user to enter their favourite film. 2. If

Task 2: Rookie 1. Ask the user to enter their favourite film. 2. If they enter the film star wars, output ‘May the force be with you!’ Flowchart Pseudo code Start OUTPUT: What is your favourite film? answer = user INPUT( ) IF answer = star wars: OUTPUT: May the force be with you! ENDIF End

Skill Task 2: Solution • Add a print screen to show your coding here.

Skill Task 2: Solution • Add a print screen to show your coding here. • Add a print screen to show your output solution here.

Skill 2: Structuring an IF-Else Statement • Often, a decision may result in one

Skill 2: Structuring an IF-Else Statement • Often, a decision may result in one action being performed or another. In programming, this is known as an IF-ELSE statement. • If the outcome of the decision is TRUE, an action will be performed. If the outcome is FALSE, an alternative action is performed. Flowchart Pseudo code START IF the Decision = TRUE THEN: Perform Action 1 ELSE: Perform Action 2 END IF END

Skill 2: IF-Else Statement Python Example This decision checks if the variable team contains

Skill 2: IF-Else Statement Python Example This decision checks if the variable team contains the string manchester united. • This programme produces two possible responses depending on what the user inputs. • Either one response or the other will be run depending on if the conditions of the IF statement are met or not. This response will only run if the decision is TRUE. Using the else function allows you to create an alternative response. If the decision is not met (FALSE), the alternative response will run. IF the statement is TRUE: IF the statement is FALSE:

Task 3: Rookie 1. Ask the user ‘what is the capital city of Wales’.

Task 3: Rookie 1. Ask the user ‘what is the capital city of Wales’. 2. If they answer cardiff, output “Correct”, else output “Wrong”. Flowchart Pseudo code START OUTPUT: What is the Capital City of Wales? answer = user INPUT( ) IF answer = cardiff: OUTPUT: Correct! ELSE: OUTPUT: Wrong! END IF END

Task 3: Solution • Add a print screen to show your coding here. •

Task 3: Solution • Add a print screen to show your coding here. • Add a print screen to show your output solution here.

Skill 3: Structuring an El. SE-IF statement • When data is checked against a

Skill 3: Structuring an El. SE-IF statement • When data is checked against a range of conditions, an IF-ELSE statement can be used. • This allows the programmer to check data against a range of conditions and perform the required action as a result. Pseudo code START score = 90 IF score <=33: OUTPUT: You are currently at Rookie level. ELSE IF score <= 66: OUTPUT: You are currently at Pro level! Good Work! ELSE IF score >66: OUTPUT: You are currently at Beast level! Fantastic work! ELSE: OUTPUT: You have not submitted a score. END IF END

Skill 3: ELSE-IF (elif) Statement Python Example • The elif statement allows you to

Skill 3: ELSE-IF (elif) Statement Python Example • The elif statement allows you to make as many TRUE or FALSE checks as you wish! • The example below shows how the variable score is checked against a range of conditions, each of which will produce a unique output depending on the value stored in score. As you can see, this programme outputs a response depending on what number is stored in the variable.

Task 4: Pro 1. 2. 3. 4. Create a username and password check programme.

Task 4: Pro 1. 2. 3. 4. Create a username and password check programme. IF the user gets the username correct then they are asked to enter their password. IF the user gets this correct they are granted access. IF at any stage they enter the wrong information, an ‘intruder alert’ message will be output! Pseudo code START OUTPUT: Please enter your username = user INPUT( ) IF username = rob-bot: OUTPUT: Please enter the password = user INPUT( ) IF password = ilovepython: OUTPUT: Access granted. Have a lovely day! ELSE: OUTPUT: Intruder alert! END IF END

Task 4: Solution • Add a print screen to show your coding here. •

Task 4: Solution • Add a print screen to show your coding here. • Add a print screen to show your output solution here.

Skill 4: Advanced condition checks • Sometimes you may wish to check more than

Skill 4: Advanced condition checks • Sometimes you may wish to check more than one values for a condition to be TRUE or FALSE. • This can be done through the use of advanced Boolean such as AND & OR. • Below is an example of how advanced Boolean can be used: Pseudo code START Chores = 5 Abdi_jobs = 3 Shelly_jobs = 5 IF Abdi_jobs = 5 AND Shelly_jobs = 5: OUTPUT: Abdi and Shelly have done all their chores for the day. ELSE: OUTPUT: There are still jobs to be finished… END IF END

Skill 4: Advanced condition checks Python example As you can see, the condition will

Skill 4: Advanced condition checks Python example As you can see, the condition will only run true if both Abdi and Shelly get all their chores done.

Task 5: Pro Practice your understanding of advanced Boolean by working out if the

Task 5: Pro Practice your understanding of advanced Boolean by working out if the pseudo code equations below will result in a TRUE or FALSE outcome. Value X Value Y 11 6 5 9 4 5 100 10 40 10 IF statement pseudo code IF X > 10 AND Y > 5: IF X < 10 AND Y > 10: IF X = 5 OR Y > X: IF Y > X AND X < 50: IF Y = 10 OR X = 10: True or False?

Task 6: Pro 1. Write a programme which asks the user to enter both

Task 6: Pro 1. Write a programme which asks the user to enter both the day and month. 2. If the date entered is the 25 th December (12 th), output “Hooray it’s Christmas day!” 3. Else if the date entered is less than 25 and the month is December (12 th), output “Christmas is just around the corner!” 4. Otherwise, output “You’ve still got a while to go until Christmas” Pseudo code START OUTPUT: Please enter the date(dd) day = user INPUT( ) OUTPUT: Please enter the month(mm) month = user INPUT( ) IF day = 25 AND month = 12: OUTPUT: Hooray! It’s Christmas Day! ELSE IF day < 25 AND month = 12: OUTPUT: Christmas is just around the corner! ELSE: OUTPUT: You’ve still got a while to go until Christmas. END IF END

Task 6: Solution • Add a print screen to show your coding here. •

Task 6: Solution • Add a print screen to show your coding here. • Add a print screen to show your output solution here.

Task 7: Pro 1. Write a programme which checks that the length of a

Task 7: Pro 1. Write a programme which checks that the length of a password entered is between 6 – 12 characters in length. 2. If the password entered is valid, output “Password accepted” 3. If the password is invalid, output “Invalid password” Pseudo code START OUTPUT: Please enter a valid password (6 -12 characters) password = user INPUT( ) pass. Len = length of password IF pass. Len < 6 OR >12: OUTPUT: Invalid password ELSE: OUTPUT: Password accepted END IF END Note: you will need to use the Python len( ) function to measure the length of the user’s password.

Task 7: Solution • Add a print screen to show your coding here. •

Task 7: Solution • Add a print screen to show your coding here. • Add a print screen to show your output solution here.

Skill 4: Nested IF Statements • Sometimes in programming the resultant action is not

Skill 4: Nested IF Statements • Sometimes in programming the resultant action is not an output or the creation of a value… it’s another IF statement! This is known a s Nesting. • This allows for more complex programming to take place, and allows for users to have a much more personalised experience. Flowchart Pseudo code START IF decision 1 = TRUE: IF decision 2 = TRUE: Perform action 1 ELSE: Perform action 2 END IF ELSE: Perform action 3 END IF END

Skill 4: Nested IF Statements Python Example Below is an example of how the

Skill 4: Nested IF Statements Python Example Below is an example of how the Nested IF statement can be used within Python. As you can see, this programme outputs a response depending on what information the user inputs:

1. 2. 3. Task 8: Beast Ask the user if they have a pet.

1. 2. 3. Task 8: Beast Ask the user if they have a pet. If they enter no, output ‘It’s OK, we can share my pet shark!’. If they enter yes ask the user for their pet’s name. If the user inputs the name speedy, tell the user ‘I used to have a pet tortoise called speedy… he ran away!’ else output ‘that’s a cool name!’ Flowchart Pseudo code START OUTPUT: Do you have a pet? answer = user INPUT( ) IF answer = yes: OUTPUT: What is your pet’s name? pet = user INPUT( ) IF pet = ‘speedy’: OUTPUT: I used to have a pet tortoise called Speedy too! But he ran away! ELSE: OUTPUT: That’s a cool name! END IF ELSE: OUTPUT: It’s OK, I’ll let you visit my pet shark! END IF END

Task 8: Solution • Add a print screen to show your coding here. •

Task 8: Solution • Add a print screen to show your coding here. • Add a print screen to show your output solution here.

Programming Challenges • Now you have learned some programming skills, it’s time to put

Programming Challenges • Now you have learned some programming skills, it’s time to put them into practice! • Complete the following programming challenges. Start with the Rookie challenge and see how far you can push yourself. • Finally, complete the debugging challenge. • Once you have finished your challenges, complete the review. Look back at the skills you have learned so far to help you solve the challenge.

Add a print screen of your solutions to the next slide. Programming Challenge: Rookie

Add a print screen of your solutions to the next slide. Programming Challenge: Rookie • Write a piece of code to ask a user what their favourite subject is. • IF their answer is computer science, output “Computer Science is awesome!” • ELSE, output “[subject] is pretty good, but Computer Science is the best!” 1 Mark Bonus mark for adding comment

Programming Challenge: Rookie - Answer • Add a print screen to show your coding

Programming Challenge: Rookie - Answer • Add a print screen to show your coding here. • Add a print screen to show your output solution here.

Programming Challenge: Pro Add a print screen of your solutions to the next slide.

Programming Challenge: Pro Add a print screen of your solutions to the next slide. • Rob-bot mortgages offers house buyers a mortgage up to four times their annual salary. • Write a programme which asks for the user’s annual salary, then multiplies it by 4. • Ask for the house price and check it against the available mortgage to see if the user will be able to afford it or not. • Ask the user if they have any deposit to put towards the house. • Output suitable responses depending on if the user can afford the house or not. 1 Mark Bonus mark for adding comment

Programming Challenge: Pro - Answer • Add a print screen to show your coding

Programming Challenge: Pro - Answer • Add a print screen to show your coding here. • Add a print screen to show your output solution here.

Programming Challenge: Beast • Write an escape the island programme. • The user must

Programming Challenge: Beast • Write an escape the island programme. • The user must escape the island by choosing the correct options at each decision junction. • There should be at least 3 decision junctions. • See the flowchart below for a structural idea: Add a print screen of your solutions to the next slide. 1 Mark per junction created (max 3) Bonus mark for adding comment

Programming Challenge: Beast - Answer • Add a print screen to show your coding

Programming Challenge: Beast - Answer • Add a print screen to show your coding here. • Add a print screen to show your output solution here.

Debugging Question Which of the following lines of code below would output the following

Debugging Question Which of the following lines of code below would output the following statement: Highlight the correct cell in green: 1 Mark

Challenges Review Complete the table below to help you identify what areas you are

Challenges Review Complete the table below to help you identify what areas you are confident at and which areas you need to improve further: Challenge Rookie Challenge Pro Challenge Beast Challenge (Max 3 marks) Debugging Challenge What Went Well: Even Better If: Your Score Unit Review