Module 3 Selection Structures 4/4/2019 CSE 1321 Module 3.

Slides:



Advertisements
Similar presentations
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Advertisements

Vahé Karamian Python Programming CS-110 CHAPTER 2 Writing Simple Programs.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 2 Writing Simple Programs
Main task -write me a program
Functions.
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
Programming Training Main Points: - Python Statements - Problems with selections.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 5 Scott Marino.
More While Loop Examples CS303E: Elements of Computers and Programming.
Branches and Program Design
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
Decision Structures, String Comparison, Nested Structures
11. EXCEPTION HANDLING Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Python Let’s get started!.
CISC105 – General Computer Science Class 8 – 06/28/2006.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Python Programming Module 3 Functions Python Programming, 2/e1.
PYTHON PROGRAMMING Year 9. Objective and Outcome Teaching Objective Today we will look at conditional statements in order to understand how programs can.
Chapter 2 Writing Simple Programs
Lesson 06: Functions Class Participation: Class Chat:
Exam #1 You will have exactly 30 Mins to complete the exam.
Python: Experiencing IDLE, writing simple programs
Python Programming Module 3 Functions Python Programming, 2/e.
Python Let’s get started!.
Introduction to Python
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Python: Control Structures
ECE Application Programming
CHAPTER 5A Loop Structure
TEMPERATURE CONVERSION
Functions.
Temperature: Comparing degrees Celsius (C) and degrees Fahrenheit (F)
How to Read a Thermometer
CMSC201 Computer Science I for Majors Lecture 04 – Decision Structures
Imperative Programming
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Decision Structures, String Comparison, Nested Structures
Types, Truth, and Expressions (Part 2)
Class 12.
Types, Truth, and Expressions (Part 2)
Count Controlled Loops
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Practice with loops! What is the output of each function below?
Lesson 06: Functions Class Chat: Attendance: Participation
How to Read a Thermometer
3. Decision Structures Rocky K. C. Chang 19 September 2018
Learn to plug in variables to solve problems.
Module 3 Selection Structures 2/19/2019 CSE 1321 Module 3.
Types, Truth, and Expressions (Part 2)
Module 3 Selection Structures 4/3/2019 CSE 1321 Module 3.
Module 4 Loops and Repetition 4/4/2019 CSE 1321 Module 4.
Class 13 function example unstring if if else if elif else
Module 2 - Part 1 Variables, Assignment, and Data Types
Module 3 Selection Structures 4/5/2019 CSE 1321 Module 3.
Objectives You should be able to describe: The while Statement
Python Basics with Jupyter Notebook
CHAPTER 5: Control Flow Tools (if statement)
Chopin’s Nocturne.
CSE 231 Lab 2.
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Functions, Procedures, and Abstraction
Imperative Programming
Python Reserved Words Poster
Debugging 101 Exterminating Vermin.
Module 3 Selection Structures 12/7/2019 CSE 1321 Module 3.
Presentation transcript:

Module 3 Selection Structures 4/4/2019 CSE 1321 Module 3

Pseudocode - IF Statement MAIN Fahrenheit ← 0, Celsius ← 0 PRINT “Enter Celsius temperature: ” READ user input Celsius ← user input Fahrenheit ← 9.0 / 5.0 * Celsius + 32 PRINT Fahrenheit IF (Fahrenheit > = 90) THEN PRINT “heat warning” ENDIF END MAIN Ps 4/4/2019 CSE 1321 Module 3

Python Example – if Statement def main(): celsius = float(input("What is the Celsius temperature? ")) fahrenheit = 9.0 / 5.0 * celsius + 32.0 print ("The temperature is", fahrenheit, "degrees Fahrenheit.") if (fahrenheit >= 90): print ("It's really hot out there!") main() # From: mcsp.wartburg.edu/zelle/python/ppics1/slides/Chapter07.ppt 4/4/2019 CSE 1321 Module 3

Pseudocode – IF-ELSE Statement MAIN Fahrenheit ← 0, Celsius ← 0 PRINT “Enter Celsius temperature: ” READ user input Celsius ← user input Fahrenheit ← 9.0 / 5.0 * Celsius + 32 PRINT Fahrenheit IF (Fahrenheit > = 90) THEN PRINT “heat warning” ELSE PRINT “there is no extreme heat” ENDIF END MAIN Ps 4/4/2019 CSE 1321 Module 3

Python if-else example celsius = float(input("What is the Celsius temperature? ")) fahrenheit = 9.0 / 5.0 * Celsius + 32 print ("The temperature is", fahrenheit, "degrees Fahrenheit.”) if (fahrenheit >= 90): print (“It's really hot out there, be careful!”) else: print (“There is no extreme heat today.”) 4/4/2019 CSE 1321 Module 3

Pseudocode – IF-ELSE-IF MAIN Fahrenheit ← 0, Celsius ← 0 PRINT “Enter Celsius temperature: ” READ user input Celsius ← user input Fahrenheit ← 9.0 / 5.0 * Celsius + 32 PRINT Fahrenheit IF (Fahrenheit > = 90) THEN PRINT “heat warning” ELSE IF (Fahrenheit >= 80) THEN PRINT “it is warm, but there is no extreme heat” ELSE IF (Fahrenheit >= 70) THEN PRINT “the temperature is pleasant and suggest a picnic” ELSE PRINT “a suggestion to take a jacket” END IF END MAIN Ps 4/4/2019 CSE 1321 Module 3

Python if-else-if example celsius = float(input("What is the Celsius temperature? ")) fahrenheit = 9.0 / 5.0 * celsius + 32 print ("The temperature is", fahrenheit, " degrees Fahrenheit.") if (fahrenheit >= 90): print ("It's really hot out there, be careful!") elif (fahrenheit >= 80): print ("It is warm, but there is no extreme heat today.") elif (fahrenheit >= 70): print ("It is very pleasant today. You should pack a picnic!") else: print ("It is cool today. You should take a jacket.") 4/4/2019 CSE 1321 Module 3

Pseudocode – switch Statement // Read user input like before conditions ← compute using Fahrenheit variable / 10 CASE conditions OF 10 : PRINT “stay inside”, BREAK 9 : PRINT “be careful due to heat”, BREAK 8 : PRINT “it is hot, but not extreme”, BREAK 7 : PRINT “pack a picnic”, BREAK DEFAULT : PRINT “take a jacket”, BREAK ENDCASE Ps 4/4/2019 CSE 1321 Module 3

Python switch example def case_10_handler(): Note: Python does not provide a switch statement, but we can implement with dictionary mapping. At this point, you may or may not be ready to try this. Remember you can accomplish the same tasks with if/elif Python switch example def case_10_handler(): print('It is too hot to go outside today. Stay inside!') def case_9_handler(): print('It is really hot out there, be careful!') def case_8_handler(): print('It is very warm, but no extreme heat today!') def case_7_handler(): print('It is very pleasant today. You should pack a picnic!') def default_handler(): print('It is cool today. You should take a jacket.') def switch_function(switch): handler = { 10: case_10_handler, 9: case_9_handler, 8: case_8_handler, 7: case_7_handler, } return handler.get(switch)() #extra parentheses for executing function #Continued on next slide 4/4/2019 CSE 1321 Module 3

Python switch example def main(): #Continued from slide 44 def main(): celsius = float(input("What is the Celsius temperature? ")) fahrenheit = 9.0 / 5.0 * celsius + 32 conditions = int(fahrenheit/10) print("It is in the ", conditions,"0's.") switch_function(conditions) if __name__== "__main__": main() 4/4/2019 CSE 1321 Module 3