session 5 conditional logic conditional logic indentation
play

Session 5 Conditional Logic Conditional Logic Indentation Matters - PowerPoint PPT Presentation

Session 5 Conditional Logic Conditional Logic Indentation Matters if a == 1: print("If a is one, this will print.") print("So will this.") print("And this.") print("This will always print because it is not


  1. Session 5 Conditional Logic

  2. Conditional Logic

  3. Indentation Matters if a == 1: print("If a is one, this will print.") print("So will this.") print("And this.") print("This will always print because it is not indented.")

  4. Basic Comparisons ● greater than less than ● ● greater than or equal to ● less than or equal to ● equal to ● not equal to

  5. Less Than/Greater Than #Example 1 # Variables used in the example ``if`` statements a = 4 b = 5 # Basic comparisons if a < b: print("a is less than b") if a > b: print("a is greater than b") print("Done")

  6. Greater/Less Than or Equal To #example 2 if a <= b: print("a is less than or equal to b") if a >= b: print("a is greater than or equal to b")

  7. Equal or Not Equal To #Example 3 # Equal if a == b: print("a is equal to b") # Not equal if a != b: print("a and b are not equal")

  8. = does not mean == # This is wrong a == 1 # This is also wrong if a = 1: print("A is one")

  9. And/Or # And if a < b and a < c: print("a is less than b and c") # Non-exclusive or if a < b or a < c: print("a is less than either b or c (or both)")

  10. Boolean Variables ● True ● False # Boolean data type. This is legal! a = True if a: print("a is true")

  11. Using And/Or with Booleans a = True b = False if a and b: print("a and b are both true")

  12. if/else statement temperature = int(input("What is the temperature in Fahrenheit? ")) if temperature > 90: print("It is hot outside") else: print("It is not hot outside") print("Done")

  13. if/elif/else temperature = int(input("What is the temperature in Fahrenheit? ")) if temperature > 90: print("It is hot outside") elif temperature < 30: print("It is cold outside") else: print("It is not hot outside") print("Done")

  14. two_truths_lie.py #print 2 truths and a lie print('Welcome to Two Truths and a Lie! /n One of the following statements is a lie...you need to identify which one!') print('1. I have a donkey living in my backyard.') print('2. I have three fur babies') print('3. I speak four languages')

  15. two_truths_lie.py #give instructions on how to guess and store the user input into a variable truth_or_lie = input('Now put in the number of the statement you think is the lie: 1, 2, or 3!') #convert the string input to an integer conv_truth_or_lie = int(truth_or_lie)

  16. two_truths_lie.py #1st condition, the variable is 1 if conv_truth_or_lie == 1: print('Congrats, you have identified the lie correctly!') #2nd condition, the variable is 2 elif conv_truth_or_lie == 2: print('That is not the one!') #3rd condition, the variable is 3 elif conv_truth_or_lie == 3: print('Sorry, you have to try again')

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend