130 likes | 276 Views
(review). Decision Structures. if condition: statement statement statement statement if mark >= 50: print(“Passed”) else: print(“Failed”). if condition: statement statement else: statement statement statement statement. (review).
E N D
(review) Decision Structures • if condition: • statement • statement • statement • statement • if mark >= 50: • print(“Passed”) • else: • print(“Failed”) • if condition: • statement • statement • else: • statement • statement • statement • statement
(review) Conditions: Relational Operators A Boolean expression results in a value of True or False.
Nested Decision Structures • if condition1: • statement • statement • else: • if condition2: • statement • statement • else: • statement • statement • statement(s) • if mark >= 80: • grade = ‘A’ • else: • if mark >=70: • grade = ‘B’ • else: • if mark >= 60: • grade = ‘C’ • else: • if mark >= 50: • grade = ‘D’ • else: • grade = ‘F’ • print(mark, grade)
For admission to university, a high school students must have taken English with a minimum grade of 70. Write the program. taken = input(“Did you take English? [Y/N]”)
Nested Decision Structures • if condition1: • statement • statement • elif condition2: • statement • statement • else: • statement • statement • statement(s) • if mark >= 80: • grade = ‘A’ • elif mark >=70: • grade = ‘B’ • elif mark >= 60: • grade = ‘C’ • elif mark >= 50: • grade = ‘D’ • else: • grade = ‘F’ • print(mark, grade)
Conditions: Boolean Operators • used with the relational operators to increase the power of the condition • allows multiple conditions to be checked within one condition • allows simple negation of a condition • three Boolean operators: not, and, or • complex conditions should use brackets to make the order of operations clear
Conditions: Boolean Operators The correct way to use the conditions: e.g. if temp >= 10 and temp < 30: print (“It is a nice day”) e.g if snack == ‘carrots’ or snack == ‘celery’: print (“Snack is healthy”) e.g. if temp > 30 and dessert = “ice cream”: print (“Good dessert for a hot day”) e.g. if (student == “international”) and (IELTS >= 6.5 or MELAB >= 85 or Pearson >= 59): print(“Met English requirements”)
Conditions: Boolean Operators • The incorrect way to use the conditions: • Case 1: an incomplete condition • if day == ‘Saturday’ or ‘Sunday’: • print (“It is the weekend!”) • will always print “It is the weekend!”. • What happens? • assume day = ‘Monday’ • day == ‘Saturday’ asks Is day equal to Saturday? • ‘Sunday’ asks Is Sunday, Sunday?
Conditions: Boolean Operators • The incorrect way to use the conditions: • Case 2: misuse of and/or • if temp > 40 and temp <= 0: • print (“Temperature extreme!”) • will never print “Temperature extreme!”. • What happens? • assume temp = 45 • temp > 40 asks Is temp greater than 40? • temp <= 0 asks Is temp less than or equal to 0?
Short-Circuit Condition Evaluation For conditions with and/or, Python stops evaluating the condition when it knows the condition has to be False. e.g. if temp >= 10 and temp < 30: print (“It is a nice day”) e.g if snack == ‘carrots’ or snack == ‘celery’: print (“Snack is healthy”)
For TOEFL we require 83 overall with a minimum of 20 in each section – reading, listening, speaking, writing. How to add TOEFL to the following check? if (student == “international”) and (IELTS >= 6.5 or MELAB >= 85 or Pearson >= 59): print(“Met English requirements”)