4. If Statements 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus

Slides:



Advertisements
Similar presentations
EasyGUI “Probably the Easiest GUI in the world”. Assumptions (Teachers’ Notes) This resources sets out an introduction to using easyGUI and Python
Advertisements

Programming for GCSE Topic 3.1: If Statements in Python T eaching L ondon C omputing William Marsh School of Electronic Engineering and Computer Science.
Identify if the statement that follow is True or False. Click your choice and see if you got the correct answer. Enjoy!
Visual Basic Statements Chapter 5. Relational Operators  OperationSymbol  Equal  =  Less than  <  Greater than  >  Not equal   Less than.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
True/False. False True Subject May Go Here True / False ? Type correct answer here. Type incorrect answer here.
Making Decisions In Python
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
Decision Making George Mason University. Today’s topics 2 Review of Chapter 2: Decision Making Go over exercises Decision making in Python.
Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Python Programming Using Variables and input. Objectives We’re learning to make use of if statements to enable code to ask questions. Outcomes Build an.
INTRODUCTION TO PYTHON. 1. The 5 operators in Python are: + - * / %
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
CSD 340 (Blum)1 Ifs. CSD 340 (Blum)2 Page that asks user for background color and changes fore color from black if user selects black as background color.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
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.
Selection Control Structure. Topics Review sequence control structure Structure theorem Selection control structure If statement Relational operators.
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
Please log on The. AN INTRODUCTION TO ‘Python is a high-level, general purpose programming language’ Python is one of the many programming languages.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Sanjay Johal. Introduction(1.1) In this PowerPoint I will be explaining :  The purpose of the code for each of the two given programs, e.g. to carry.
James Tam Making Decisions In Python In this section of notes you will learn how to have your programs choose between alternative courses of action.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
Decision Making CMSC 201 Chang (rev ).
5. Loops 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
7. Lists 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
1. Starting 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Let’s Learn 3. Modules Saenthong School, January – February 2016
Learning Objective To be able to… Understand flow chart symbols Complete and correct flow chart algorithms Create a program based on a flow chart.
8. Functions 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Introduction to Programming Python Lab 7: if Statement 19 February PythonLab7 lecture slides.ppt Ping Brennan
Introduction to Programming Python Lab 6: Relational Operators and Boolean Variables 12 February PythonLab6 lecture slides.ppt Ping Brennan
Decisions (If Statements) And Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.
11. Skier Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
1. Games Background Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
3. Drawing Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
9. Media (sound effects, music, video) Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
PYTHON IF-STATEMENTS. What you know If something is true, then something happens Example If you heat water to 100 degrees Celsius, then it boils If it.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
4. If Statements 1 Have your program make choices. Depending on a test have a program do different things Computer Programming BSc in Digital.
6. Classes & Objects Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
Let’s Learn 12. Packaging a Game Saengthong School, June – August 2016
Let’s Learn 2. Installing Pygame
4. If Statements Let's Learn Python and Pygame
What are variables? Using input()
How we keep track of the truthiness
Let's Learn Python and Pygame
Types, Truth, and Expressions (Part 2)
Let’s Learn 6. Nested Loops Saenthong School, January – February 2016
8. Starting Pygame Let's Learn Python and Pygame
Types, Truth, and Expressions (Part 2)
6. Lists Let's Learn Python and Pygame
Teaching London Computing
Types, Truth, and Expressions (Part 2)
COMPUTER PROGRAMMING PYTHON
7. Functions Let's Learn Python and Pygame
Logical Operations In Matlab.
Python 21 Mr. Husch.
Conditional and iterative statements
What are variables? Using input()
Relational & Logical Operators, if and switch Statements
X Bad 1 Bad 2 Bad 3 Good 1 Good 2 Good 3 Question
Types, Truth, and Expressions (Part 2)
Programming In Lesson 4.
Making decisions with code
What are variables? Using input()
Starter Activities GCSE Python.
Presentation transcript:

4. If Statements 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus Have your program make choices. Depending on a test have a program do different things.

 If Andrew got the right answer, add 1 point to his score.  If Jane hit the alien, make an explosion sound.  If the file isn’t there, display an error message. If statement: test, action(s) 2

 If Andrew got the right answer, add 1 point to his score. "If" as Rail Lines 3 Test: Did Andrew get the right score? Action: Add 1 point to his score

age1.py 4

print("Enter your age: ", end =" ") age = int(input()) if age > 16: print("You are Old, ") print("Old, " * age) Code Details 5 test: is age > 16? actions must be indented (add 4 spaces at the start of each line)

Test Operations 6

Comparison of Strings  Comparison operators work for strings >>> "David" < "David Cameron" True >>> "Dave" < "David" True >>> "Tom" < "Thomas" False >>> "Bill" < "William" True >>> "AAA" < "AAB" True >>> "AAA" < "aaa" True >>> "aaa" < "AAA" False Uses A-Z a-z ordering, letter by letter

8 age2.py

age2.py as Rail Lines 9 age > 16? age >= 12 ? age >= 8 ? Old, old, … Not a bad age Get lost kid Baby

age3.py 10

age3.py as Rail Lines 11 age > 16? Old, old, … age >= 12? age >14 ? Perfect Not a bad age Get lost

 The train lines rejoin so Python can execute the next lines of your program. What Happens after the if? 12 more code here

 Use the words: and, or, not to build more complex tests:  age >= 12 and age <= 14  color == "red" or color == "blue"  not name == "Andrew"  same as name != "Andrew"  These words can be combined to make even more complicated tests:  e.g. (name == "Andrew" and age > 18) or (age < 16) Making Complex Tests 13

Complex Test Operations 14 OperatorExampleResult and(2 == 3) and (-1 < 5)False or(2 == 3) or (-1 < 5)True notnot (2 == 3)True

Truth Table for or rate = -1 print rate 100 ABA or B True FalseTrue FalseTrue False Answer?

Truth Table for or rate = 160 print rate 100 ABA or B True FalseTrue FalseTrue False Answer?

Truth Table for or rate = 50 print rate 100 ABA or B True FalseTrue FalseTrue False Answer?

Truth Table for and rate = -1 print rate >= 0 and rate <= 100 ABA and B True False TrueFalse Answer?

Truth Table for and rate = 50 print rate >= 0 and rate <= 100 ABA and B True False TrueFalse Answer?

Truth Table for and rate = 160 print rate >= 0 and rate <= 100 ABA and B True False TrueFalse Answer?

age4.py 21