4. If Statements Let's Learn Python and Pygame

Slides:



Advertisements
Similar presentations
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.
Advertisements

Lecture 11 – if … else, if... elif statements, nested ifs COMPSCI 1 1 Principles of Programming.
INTRODUCTION TO PYTHON. 1. The 5 operators in Python are: + - * / %
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.
Lecture 10 – Boolean expressions, if statements COMPSCI 101 Principles of Programming.
COMPSCI 101 Principles of Programming Lecture 25 – Nested loops, passing mutable objects as parameters.
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.
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Decision Making CMSC 201 Chang (rev ).
3.1.3 Program Flow control Structured programming – SELECTION in greater detail.
Python: Selection Damian Gordon. Python: Selection We’ll consider two ways to do selection: The IF statement The CASE statement.
7. Lists 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
4. If Statements 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
 Type Called bool  Bool has only two possible values: True and False.
4. If Statements 1 Have your program make choices. Depending on a test have a program do different things Computer Programming BSc in Digital.
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
Selection: Non-linear programs
Lesson 4 - Challenges.
Lesson 04: Conditionals Topic: Introduction to Programming, Zybook Ch 3, P4E Ch 3. Slides on website.
Programming Mehdi Bukhari.
Introduction to Programming
Chapter 4: Decision Structures and Boolean Logic
5. Loops Let's Learn Python and Pygame
CSC115 Introduction to Computer Programming
What are variables? Using input()
Conditionals (if-then-else)
Let's Learn Python and Pygame
Introduction to Programming
And now for something completely different . . .
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
10. User Input Let's Learn Python and Pygame
Types, Truth, and Expressions (Part 2)
6. Lists Let's Learn Python and Pygame
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
BSc in Digital Media, PSUIC
Chapter 4: Decision Structures and Boolean Logic
Introduction to Programming
7. Functions Let's Learn Python and Pygame
Program Flow Control Selection & repetition
Introduction to Programming
Comparing Strings Strings can be compared using the == and != operators String comparisons are case sensitive Strings can be compared using >, =, and.
Chapter 5: Control Structure
Conditional and iterative statements
What are variables? Using input()
A look at Python Programming Language 2018.
Python programming exercise
Adding Intelligence Check for the presence or absence of a condition in the environment Take the appropriate actions Involves making a choice (to do or.
Types, Truth, and Expressions (Part 2)
Selection Structures: Single-alternative
Programming In Lesson 4.
CHAPTER 5: Control Flow Tools (if statement)
Combining conditions with and, or, and not.
Introduction to Programming
What are variables? Using input()
Types, Truth, and Expressions
Introduction to Programming
CSE 231 Lab 2.
2-2 Logic Part 2 Truth Tables.
Chapter 4: Decision Structures and Boolean Logic
CMPT 120 Lecture 6 – Unit 1 – Chatbots
Types, Truth, and Expressions (Part 2)
Flow Control I Branching and Looping.
Presentation transcript:

4. If Statements Let's Learn Python and Pygame Aj. Andrew Davison, CoE, PSU Hat Yai Campus E-mail: ad@fivedots.coe.psu.ac.th 4. If Statements Have your program make choices. Depending on a test have a program do different things.

If statement: test, action(s) 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" Flowchart If Andrew got the right answer, add 1 point to his score. executing program Test: Did Andrew get the right score? no yes Action: Add 1 point to his score execution continues...

age1.py

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

Flowchart executing program Test: no age > 16? yes Action: print old execution continues...

Test Operations

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

If and Else print("Enter your age: ", end =" ") age = int(input()) if age > 16: print("You are Old, ") print("Old, " * age) else: print("You are Young")

Flowchart executing program Test: no age > 16? yes Action: print young Action: print old execution continues...

Not quite what we want! Try with 65 Many ifs No Yes grade>70 print "A" if (grade > 70): print("A") if (grade > 60): print("B") if (grade > 50): print("C") else: print("D") No Yes grade>60 print "B" No Yes grade>50 print "D" print "C"

Elifs ("else if"s) No Yes grade>70 print "A" if (grade > 70): print("A") elif (grade > 60): print("B") elif (grade > 50): print("C") else: print("D") No Yes grade>60 print "B" No Yes grade>50 print "D" print "C" Yes! Try with 65, 72, 40, ...

age2.py

age2.py Flowchart Yes! Try with 14, 7, 10, 9, ... No Yes age>16 "old" No Yes age>=12 "not bad" No Yes age>=8 "baby" "get lost" Yes! Try with 14, 7, 10, 9, ...

age3.py

age3.py Flowchart Yes! Try with 14, 7, 10, 9, ... No Yes age>16 "old" No Yes age>=12 No Yes age>14 "get lost" "not bad" "perfect" Yes! Try with 14, 7, 10, 9, ...

Complex Test Operations Operator Example Result and (2 == 3) and (-1 < 5) False or (2 == 3) or (-1 < 5) True not not (2 == 3) and: True only if both tests are true or: True if one or both tests are true not: True if test is false

1. Truth Table for or print True if one or both are true Answer? A B rate = -1 rate < 0 or rate > 100 A B A or B True False Answer?

2. Truth Table for or print True if one or both are true Answer? A B rate = 160 rate < 0 or rate > 100 A B A or B True False Answer?

3. Truth Table for or print True if one or both are true Answer? A B rate = 50 rate < 0 or rate > 100 A B A or B True False Answer?

1. Truth Table for and print True only if both are true Answer? A B rate = -1 rate >= 0 and rate <= 100 A B A and B True False Answer?

2. Truth Table for and print True only if both are true Answer? A B rate = 50 rate >= 0 and rate <= 100 A B A and B True False Answer?

3. Truth Table for and print True only if both are true Answer? A B rate = 160 rate >= 0 and rate <= 100 A B A and B True False Answer?

Summary of or, and or: True if one or both tests are true and: True only if both tests are true

age4.py and: True only if both tests are true

age4.py Flowchart and: True only if both tests are true No Yes "old" age>=10 and age <= 14 and: True only if both tests are true No Yes "get lost" "perfect