Selection and Python Syntax If, Elif, Else Selection and Python Syntax
Learning intentions and Outcomes Learning Intention and Context Graded Learning Outcomes To develop an understanding of how to perform selection in a python program Context: Programming in Python Grade 4 - 5 Use an IF statement to perform selection on a range of Python programs Grade 6 Use nested IF statements to perform binary tree selection in a range of Python programs Grade 7 Research iteration using count controlled loops to iterate a program a set number of times Grade 8 Produce a detailed analysis of how a problem is solved alongside detailed algorithms to demonstrate the solution. Programming the solution to a problem using iteration and selection statements.
Decision in python: if, elif, else Sometimes we only want a program to execute code under certain circumstances. We call this selection. The most commonly used way of doing this is using if. Here we say if a condition is true or false (Boolean Logic) execute some code. First we need to make sure we are happy what a condition is. A Boolean condition is one that can have only two values true or false.
Boolean values Operator Meaning == Equals != Does not Equal > Greater Than < Less Than >= Greater Than or Equal to <= Less Than or Equal to
if, elif, else If is used to start a decision block in Python ThrowDice = number between 1 and 6 Counter starts at space 0 if Counter on SnakeHead: Slide Down Snake elif Counter on BottomLadder: Move up Ladder else: End Turn If is used to start a decision block in Python Elif is used to continue each subsequent decision Else is used at the end of a decision block
Example: Guess a number print("Let's play a game! Please enter a number between 50 and 60") UserNumber = int(input("Enter your number now! ")) if UserNumber <= 50: print("Sorry that is too low, it should be between 50 and 60") elif UserNumber >= 60: print("Sorry that is too high, it should be between 50 and 60") else: print("You guessed right")
Entry Ticket: IF, ELIF, ELSE 2 Write a program that asks the user to enter a number between 50 and 100. If the user enters a valid number it should print Valid If the user enters a number that is too high it should print Invalid – Too High If the user enters a number that is too low it should print Invalid – Too Low Put your code into Python and run it Python Programming Challenges – start with Challenge 9
Exit Ticket: Control Flow What are the following terms used for? Identify where in the following program the variable Score changes: If Draw a circle around every process box that the variable changes in Elif What is the maximum possible score? Else How many iterations can the program run for? _________________________ _________________________ ______________ _________________________ ______________ Name
Entry Ticket: Printing with Variables 2 Write a program that asks the user what town they live in. The program should then print “I love visiting” TOWN Put your code into Python and make sure it runs Challenge 5 of the programming challenges
Exit Ticket: Printing and Data Types What is a Variable? What is wrong with the following code? Circle the problems and rewrite the code to make it work. What can be in a string data type? What Data type is 3.14? What Data type is 21? myName = “Mr Petford” Print(myName) myAge = 10 + 9 MyAge = str(21) print(myage) mySchool = int(“Haybridge”) print(myHome) Name