Professor: Sana Odeh Lecture 5 Control Structure: making decisions with if, else and elif

of 30

Please download to get full document.

View again

All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
PDF
30 pages
0 downs
2 views
Share
Description
1 Professor: Sana Odeh Lecture 5 Control Structure: making decisions with if, else and elif 2 Boolean data types (True and False values) Python and other programming languages provide
Transcript
1 Professor: Sana Odeh Lecture 5 Control Structure: making decisions with if, else and elif 2 Boolean data types (True and False values) Python and other programming languages provide a boolean data type that has the value of true and false evaluation. History: George Boole, the English mathematician who published An investigation into the Laws of Thought in 1854 which began Boolean logic. Boolean logic is the basis of modern digital computer logic. Any Boolean expressions always evaluate to either True or False. Allows programs to make decisions executing specific statements when condition evaluates to True and different statements when condition evaluates to false Provides a control over the flow of the programs # Prof Sana Odeh / V / New York University, all material is copy righted! 3 Relational/logical Operators used for conditions to compare operands Operator = = ==!= Meaning Greater than Less than Greater than or equal to Less than or equal to Equal to Not Equal to 4 Lets look at how boolean values work # Boolean data type variables have value of True and False print (5 == 6) print (5 6) print (5 6) print (5 = 6) print (5 = 6) print (5!= 6) # Prof Sana Odeh / V / New York University, all material is copy righted! 5 if statements Conditions using if statements Boolean expressions used for condition ( ifstatement) to make decisions: if age 13: print ( Your age is, age, You are a child! ) Notice the syntax: There is a colon after the condition The statement(s) following the colon that relate to the if statement are indented to form a block of Code # Prof Sana Odeh / V / New York University, all material is copy righted! Testing for Equality operator (==) vs. Assignment (=) 6 Remember that one equals sign ( = ) is not testing for equality (it allows a variable to point to a value) grade = 100 To test for equality: We really want to use == if grade == 90: print( You got an A ) 7 Introduction to Problem Solving with Computers Before writing a program: Have a deep understanding of the problem Develop a solution (simple, efficient, easy to read, concise ) 8 Pseudocode Pseudocode Similar to everyday English It s not needed by the interpreter Benefits for programmer: To develop a solution without worrying about syntax Think freely (helpful to develop creative and efficient solutions) Pseudocode (let s refine it but keep it brief and simple) Here is an example of Pseudocode to determine letter grade: 1) Enter your grade Enter grade as float: ) Test to see if grade is an A, B, C, D, or F Use nested if, elif, else statements if grade = 60 then print( You passed!!! ) print( you failed! ) 9 Pseudocode (Now, let s develop a python equivalant) 1) Enter your grade Enter grade as float: ) Test to see if grade is an A, B, C, D, or F Use nested if, elif, else statements if grade = 60 then print( You passed!!! ) else print( you failed! ) * Translate above pseudocode into python: grade = int(input('enter your grade: ')) if grade = 60: print( You passed!!! ) else: print( you failed! ) 10 11 if with a twist: if else Basically is this (pseudocode): if some Boolean expression is true do this otherwise do something else Example: if grade = 65: else: print( You passed! ) print( You failed! ) 12 Blocks To run several lines of code together, you must include them within a block by providing same indentations (after if or else or elif) For example: if grade = 60: print( You passed! ) print( Congratulations! ) 13 Indentation Everything within the block of code (even if it is an implicit block because we only use one statement) should be indented Otherwise you get a syntax error if grade = 60: print( you passed!!!\n ) print( congratulations!\n ) This is valid Python code (indentation convey structure in python.. You must indent after if, else, or elif (else if). 14 What is a Flow Chart? A flow chart is a visual tool that helps you understand the flow of your program. Graphical representation of program structure Different shapes have different meaning. 15 Flow Chart Basics 1 Connector symbol flow line Diamonds (decision symbol) contain conditions flow line Rectangles represent statements of work. For example: print() Flow Chart Basics 1 for control structure (if, elif, else) Connector symbol 16 Flow control structure print( You failed!!! )) If False if grade = 60: If True flow line print( You passed!!! )) 17 if/else Flow Chart False Print You failed grade =60 True Print You passed Control Structures 18 19 Control Structures Control the flow of a program So far, we executed statements in sequential order (in the order that it appears) But, we need to be able to make decisions to execute specific statements if conditions are true and other statements if condition is false If it rains, then use an umbrella else leave it at home This is called Control structures: allows the programmer to control the flow of the program 20 Three Basic Control Structures All programs can be written with just these types of structures Sequence structure (simple) Statements run one after the other (natural order: execute statements in the order that they appear- no skipping) Selection structure (control flow of a program using conditions) Depending on a condition, do one thing otherwise, do something else Examples in Python : if, if else, and switch. Repetition structure (loops) Repeat some actions over and over Examples in Python : for loops, while loops, and do/while loops. 21 The if structure Pseudocode: if some Boolean expression is true do this Example: if age == 21: print ( You can have a drink legally! ) Every programming language has some form of an if statement. 22 Another if example If temperature = 85: print( It is hot out! ) else: print( It is cold out! ) What do you think? 23 Temperature example # checking temperature! - wk3_temp.py temp = input('enter temperature: ') temperature = int(temp) if temperature 60: print( It is cold out! ) elif temperature = 79: print( It is comfortable out! ) else: print( It is hot out! ) 24 if Flow Chart temperature =85 true print It is hot false 25 # Prof Sana Odeh / New York University # Determine grade! - wk3_grade.py grade = int(input('enter your grade: ')) Example if grade = 60: print( You passed!!! ) print( Congratulations! ) else: print( you failed! ) 26 # Prof Sana Odeh / / New York University # Determine grade! - wk3_grade2.py grade = int(input('enter your grade: ')) Example if grade = 90: print( you got an \ A\ .\n ) elif grade = 80: print( you got a \ B\ .\n ) elif grade = 70: print( you got a \ C\ .\n ) elif grade = 60: print( you passed but you need a tutor.\n ) elif grade = 50: print( you Falied.\n ) else: print( You entered an Invalid data.\n ) 27 # Determine grade! - wk3_grade3.py Example grade = float(input('enter your grade: ')) if grade = 90 and grade = 100: print( you got an \ A\ .\n ) elif grade = 80 and grade = 89: print( you got a \ B\ .\n ) elif grade = 70 and grade = 79: print( you got a \ C\ .\n ) elif grade = 60 and grade = 69: print( you passed but you need a tutor.\n ) else: print( you failed.\n ) 28 Truth table for logical and and or operators Using the Logical and or True and True True True and False False True or False True False or True True # Prof Sana Odeh / V / New York University, all material is copy righted! 29 Formatting output using %d and %f and %s # program contune.. : print this out... line by line. print ( ,number1) print (' ',number2) print (' ',number3) print (' ',number4) print ('',number5) print () print(( *4),number1) print(( *3),number2) print( *2,number3) print( *1,number4) print( ,number5) print () To format float numbers (how many decimal values and digits to print, use %f (ex. If num = then print( num is %8.2f % num) will produce num is To format int numbers (how many digits and places to print, use %D To format string (how many characters and places to print, use %S print( %8.1f % number1) print( %9.2f % number2) print( %10.3f % number3) print( %11.4f % number4) print( %12.5f % number5) print () 30 Formatting output using %d and %f and %s n1 = str(number1) n2 = str(number2) n3 = str(number3) n4 = str(number4) n5 = str(number5) num = 20 print( %5d % num) print( %8s % n1) print( %9s % n2) print( %10s % n3) print( %11s % n4) print( %12s % n5) print ('\n') print(( + *2)) print(( cat *4))
Related Search
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks