1 / 12

Decision Structures

(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).

Télécharger la présentation

Decision Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. (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

  2. (review) Conditions: Relational Operators A Boolean expression results in a value of True or False.

  3. 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)

  4. 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]”)

  5. 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)

  6. 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

  7. Conditions: Boolean Operators

  8. 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”)

  9. 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?

  10. 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?

  11. 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”)

  12. 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”)

More Related