Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Module 3 Selection Structures 4/4/2019 CSE 1321 Module 3."— Presentation transcript:

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

2 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

3 Python Example – if Statement
def main(): celsius = float(input("What is the Celsius temperature? ")) fahrenheit = 9.0 / 5.0 * celsius 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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


Download ppt "Module 3 Selection Structures 4/4/2019 CSE 1321 Module 3."

Similar presentations


Ads by Google