Python: Selection Damian Gordon. Python: Selection We’ll consider two ways to do selection: The IF statement The CASE statement.

Slides:



Advertisements
Similar presentations
Python - Selection Starter
Advertisements

If Statements, Try Catch and Validation. The main statement used in C# for making decisions depending on different conditions is called the If statement.
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Flow Charting, Structured English and PseudoCode
Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of.
1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.
Greater than or equal? A lesson on math language A lesson on math language.
Chapter 9 IF Statement Bernard Chen. If Statement The main statement used for selecting from alternative actions based on test results It’s the primary.
Chapter 9 IF Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
An introduction to Python A series of introductory lessons to Python that does not use too much maths!
Python: Modularisation Damian Gordon. Modularisation Remember the prime checker program:
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.
Selection: IF Statement Damian Gordon. adsdfsdsdsfsdfs dsdlkmfsdfmsdl kfsdmkfsldfmsk dddfsdsdfsd.
Dealing with Errors. Error Types Syntax Errors Runtime Errors Logical Errors.
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.
Xin Liu Feb 11, * Part 1 1. What value does s have, after this code is run? s = '*' s = s + s s = s + s + s (A) '**' (B) '***' (C) '****' (D) '*****'
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
Python: Print Damian Gordon. Your first Python program When learning a new computer programming language, the first thing typically taught is how to write.
Conditions in Java. First…Boolean Operators A boolean data type is always true or false. Boolean operators always return true or false For example: (x.
Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments.
Python: Arrays Damian Gordon. Arrays In Python arrays are sometimes called “lists” or “tuple” but we’ll stick to the more commonly used term “array”.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Prime Numbers Damian Gordon. Prime Numbers So let’s say we want to express the following algorithm: – Read in a number and check if it’s a prime number.
Decision Making CMSC 201 Chang (rev ).
Python if.
3.1.3 Program Flow control Structured programming – SELECTION in greater detail.
Some More Python Please ensure that you’re logged on and ready to start.
Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version of the prime number checking program:
Python: Iteration Damian Gordon. Python: Iteration We’ll consider four ways to do iteration: – The WHILE loop – The FOR loop – The DO loop – The LOOP.
Introduction to Python Damian Gordon e:
Python: Exception Handling Damian Gordon. Exception Handling When an error occurs in a program that causes the program to crash, we call that an “exception”
Python: Menu-Driven Programs Damian Gordon. Menu-Driven Programs These are programs that display a menu (or list of options), and allows the user to choose.
Selection: CASE Statement Damian Gordon. Selection As well as the IF Statement, another form of SELECTION is the CASE statement.
Introduction to Python
Introduction to Programming
Lesson 3 - Repetition.
4. If Statements Let's Learn Python and Pygame
Iterations Programming Condition Controlled Loops (WHILE Loop)
While Loops (Iteration 2)
Introduction to Programming
Boolean Logic Damian Gordon.
Multiple Selections (ELIF Statements)
Introduction to Programming
Program Flow Control Selection & repetition
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Python: Algorithms Damian Gordon.
Suggested Layout ** Designed to be printed on white A3 paper.
Some Common Issues: Iteration
Inputs and Variables Programming Guides.
Conditional and iterative statements
Python programming exercise
An introduction to Python
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.
Python ~ Control Structure
Module 3 Selection Structures 4/4/2019 CSE 1321 Module 3.
Decisions In today’s lesson we will look at:
What is x? x = 12 %
Some Common Issues: Print, Maths, Variables
CHAPTER 5: Control Flow Tools (if statement)
Python: Sorting – Selection Sort
Some Common Issues: Common Algorithms
Introduction to Programming
CSE 231 Lab 2.
Python Conditionals: The if statement
Control Flow statements
CMPT 120 Lecture 6 – Unit 1 – Chatbots
What does this code do? def digitsum(): code goes here def diffsum():
Presentation transcript:

Python: Selection Damian Gordon

Python: Selection We’ll consider two ways to do selection: The IF statement The CASE statement

Python: IF statement Damian Gordon

Python: IF statement In Python the general form of the IF statement is as follows: if CONDITION: STATEMENT(S) else: STATEMENT(S)

Python: IF statement But we’ll do: if CONDITION: # THEN STATEMENT(S) else: STATEMENT(S) # ENDIF;

# PROGRAM SimpleIfStatement: x = 6 y = 7 if x > y: # THEN print(“x is bigger”) else: print(“y is bigger”) # ENDIF; # END.

Python: IF statement Let’s get the user to input the values of x and y:

# PROGRAM AnotherSimpleIfStatement: x = int(input()) y = int(input()) if x > y: # THEN print(x, “is bigger than”, y) else: print(y, “is bigger than”, x) # ENDIF; # END.

Python: IF statement Let’s add some PRINT statements to make this clearer:

# PROGRAM AnotherSimpleIfStatementPrints: print(“Please input the first value”) x = int(input()) print(“Please second the second value”) y = int(input()) if x > y: # THEN print(x, “is bigger than”, y) else: print(y, “is bigger than”, x) # ENDIF; # END.

Python: IF statement We can make this shorter:

# PROGRAM AnotherSimpleIfStatementPrintsShorter: x = int(input(“Please input the first value\n”)) y = int(input(“Please second the second value\n”)) if x > y: # THEN print(x, “is bigger than”, y) else: print(y, “is bigger than”, x) # ENDIF; # END.

Python: IF statement Lets try the Odd or Even program:

# PROGRAM IsOddOrEven: x = int(input(“Please input the number\n”)) if (x % 2) != 0: # THEN print(x, “is odd”) else: print(x, “is even”) # ENDIF; # END.

OperatorDescription != is not equal to == is equal to > is greater than < is less than >= is greater than or equal to <= is less than or equal to

Python: IF statement Let’s try the bigger of three numbers:

# PROGRAM BiggerOfThree: a = int(input(“Please input the first value\n”)) b = int(input(“Please second the second value\n”)) c = int(input(“Please second the third value\n”)) if a > b: # THEN if a > c: # THEN print(a, “is bigger than”, b, “ and ”, c) else: print(c, “is bigger than”, a, “ and ”, c) # ENDIF; else: if b > c: # THEN print(b, “is bigger than”, a, “ and ”, c) else: print(c, “is bigger than”, a, “ and ”, b) # ENDIF; # END.

Python: CASE statement Damian Gordon

Python: CASE statement Python doesn’t support a CASE statement But it does have a special form of IF statement that uses ELIF instead of ELSE.

# PROGRAM BiggerOfThree: a = int(input(“Please input the first value\n”)) b = int(input(“Please second the second value\n”)) c = int(input(“Please second the third value\n”)) if a > b: # THEN if a > c: # THEN print(a, “is bigger than”, b, “ and ”, c) else: print(c, “is bigger than”, a, “ and ”, c) # ENDIF; else: if b > c: # THEN print(b, “is bigger than”, a, “ and ”, c) else: print(c, “is bigger than”, a, “ and ”, b) # ENDIF; # END.

# PROGRAM BiggerOfThree: a = int(input(“Please input the first value\n”)) b = int(input(“Please second the second value\n”)) c = int(input(“Please second the third value\n”)) if a > b: # THEN if a > c: # THEN print(a, “is bigger than”, b, “ and ”, c) else: print(c, “is bigger than”, a, “ and ”, c) # ENDIF; else: if b > c: # THEN print(b, “is bigger than”, a, “ and ”, c) else: print(c, “is bigger than”, a, “ and ”, b) # ENDIF; # END.

# PROGRAM BiggerOfThreeElif: a = int(input(“Please input the first value\n”)) b = int(input(“Please second the second value\n”)) c = int(input(“Please second the third value\n”)) if a > b: # THEN if a > c: # THEN print(a, “is bigger than”, b, “ and ”, c) else: print(c, “is bigger than”, a, “ and ”, c) # ENDIF; elif b > c: # THEN print(b, “is bigger than”, a, “ and ”, c) else: print(c, “is bigger than”, a, “ and ”, b) # ENDIF; # END.

Python: IF-ESIF statement In Python the general form of the IF-ESIF statement is as follows: if CONDITION: STATEMENT(S) elif CONDITION: STATEMENT(S) elif CONDITION: STATEMENT(S) else: STATEMENT(S)

Python: IF-ESIF statement But we’ll do: if CONDITION: # THEN STATEMENT(S) elif CONDITION: # THEN STATEMENT(S) elif CONDITION: # THEN STATEMENT(S) else: STATEMENT(S) # ENDIF;

Python: IF-ESIF statement Let’s look at doing a multi-choice question program:

# PROGRAM MultiChoiceQuestion: InputValue = input("Please input your answer:\n") if InputValue == "a": # THEN print("Wrong Answer") elif InputValue == "b": # THEN print("Wrong Answer") elif InputValue == "c": # THEN print("Right Answer") elif InputValue == "d": # THEN print("Wrong Answer") else: print("Bad Option") # ENDIF; # END.

Python: IF-ESIF statement Here’s how to calculate a grade:

# PROGRAM GetGrade: InputValue = int(input("Please input the first value\n")) if InputValue >= 70: # THEN print("It's a first") elif InputValue >= 60: # THEN print("It's a 2.1") elif InputValue >= 50: # THEN print("It's a 2.2") elif InputValue >= 40: # THEN print("It's a third") else: print("Dude, sorry, it's a fail") # ENDIF; # END.

etc.