Selection and Python Syntax

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Chapter 4 - Control Statements
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Conditional Statements Introduction to Computing Science and Programming I.
Geography 465 Assignments, Conditionals, and Loops.
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
CHAPTER 3 Decisions and Repetition. A few new constants Constants so far:  Strings  Numbers  integer  float Boolean constants: [On board]
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Decision Structures, String Comparison, Nested Structures
Logical Operators, Boolean Variables, Random Numbers This template was just too good to let go in one day!
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Decision Making CMSC 201 Chang (rev ).
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 3 Decision Trees Conditionals.
3.1.3 Program Flow control Structured programming – SELECTION in greater detail.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
For Loop GCSE Computer Science – Python. For Loop The for loop iterates over the items in a sequence, which can be a string or a list (we will discuss.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
Selection Using IF THEN ELSE CASE Introducing Loops.
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
Control Flow (Python) Dr. José M. Reyes Álamo.
A Simple Quiz for Alice 3.2:
Topic: Iterative Statements – Part 1 -> for loop
Week 4 Computer Programming Gray , Calibri 24
Entry Ticket: Algorithms and Program Construction
Topic: Conditional Statements – Part 2
Python: Control Structures
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Topics The if Statement The if-else Statement Comparing Strings
Learning to Program in Python
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Learning to Program in Python
Control Structure Senior Lecturer
Lesson 8: Boolean Expressions and "if" Statements
Learning to Program in Python
Decision Structures, String Comparison, Nested Structures
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
A Simple Quiz for Alice 3.2:
Repetition Structures
Week 4 Computer Programming Year 9 – Unit 9.04
3. Decision Structures Rocky K. C. Chang 19 September 2018
Visual Basic – Decision Statements
Selection Statements.
Comparing Strings Strings can be compared using the == and != operators String comparisons are case sensitive Strings can be compared using >, =, and.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Conditional and iterative statements
Topics The if Statement The if-else Statement Comparing Strings
PYTHON: BUILDING BLOCKS Sequencing & Selection
CHAPTER 6: Control Flow Tools (for and while loops)
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
CHAPTER 5: Control Flow Tools (if statement)
Another Example Problem
Lecture 7 – Unit 1 – Chatbots Python – For loops + Robustness
Starter Look at the hand-out.
Iteration – While Loops
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Flow Control I Branching and Looping.
Flow Control II While Loops.
Presentation transcript:

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