1 / 13

Making Decisions in Python

Making Decisions in Python. Sec 9-10 Web Design. Objectives. The student will: Understand how to make a decision in Python Understand the structure of an if-statement Understand conditional expressions Understand Boolean operations. Making Decisions – Controlling Program Flow.

tehya
Download Presentation

Making Decisions in Python

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. Making Decisions in Python Sec 9-10 Web Design

  2. Objectives The student will: • Understand how to make a decision in Python • Understand the structure of an if-statement • Understand conditional expressions • Understand Boolean operations

  3. Making Decisions – Controlling Program Flow • To make interesting programs, you must be able to make decisions about data and take different actions based upon those decisions. • The IF statement allows you to conditionally execute a block of code. • The syntax of the IF statement is as follows: if ( boolean expression): STATEMENT STATEMENT • The indented block of code following an if statement is executed if the boolean expression is true, otherwise it is skipped.

  4. Conditions (aka Boolean Expressions) • All conditions result in either of two values: • True or False (or, alternately a 1 or a 0). • These are Python values, just like numbers. • Simple conditions can be written using comparison (or relational) operations: < (less than) <= (less than or equal to) > (greater than) >= (greater than or equal to) == (equal to) != (not equal to)

  5. Conditional Expression - Examples >>> 42 > 23 True >>> 42 < 23 False >>> 42 == 23 False >>> 42 != 23 True >>> (42 + 23) < 100 True >>> a, b, c = 10, 20, 10 >>> a == b False >>> a == c True >>> a == a True

  6. IF statement - example numberOfWheels = 3 if ( numberOfWheels < 4): print "You don't have enough wheels!" print "I'm giving you 4 wheels!" numberOfWheels = 4 print "You now have", numberOfWheels, "wheels" • The last print statement is executed no matter what. The first two print statements and the assignment of 4 to the numberOfWheels is only executed if numberOfWheels is less than 4.

  7. Logical Operations • Besides relational operations you can build more complex conditional expressions using the logical operations (also called Boolean operations): and, or, and not. Here are some examples: >>> (5 < 7) and (8 > 3) True >>> not ( (5 < 7) and (8 > 3)) False >>> (6 > 7) or (3 > 4) False >>> (6 > 7) or (3 > 2) True

  8. IF/ELSE • If you have two mutually exclusive choices, and want to guarantee that only one of them is executed, you can use an IF/ELSE statement. The ELSE statement adds a second block of code that is executed if the boolean expression is false. if ( boolean expression ): STATEMENT STATEMENT else: STATEMENT STATEMENT

  9. IF/ELSE statement - example numberOfWheels = 3 if ( numberOfWheels < 3): print "You are a motorcycle!" else: print "You are a Car!" print "You have", numberOfWheels, "wheels" • The last print statement is executed no matter what. If numberOfWheels is less than 3, it's called a motorcycle, otherwise it's called a car!

  10. Let’s Play a game

  11. Summary • Decisions are what programs interesting and able to adapt. • Conditionals allow you to choose what code to execute.

  12. Rest of Today • Program the game Morra (also called Throwing fingers) • Morra is a hand game that goes back thousands of years to ancient Roman and Greek times. • Each player holds one closed fist over an open hand. Players chant together "one, two, three, shoot!" as they smack their closed fists on top of their open hands. By the time they get to "shoot" they should be moving their fists in time with each other. When the players say "shoot," they stick either one or two fingers out of their closed fist. Now count the total number of fingers sticking out on the players. If it's odd, the odd player wins. If it's even, the even player wins.

  13. Rest of Today • Program the game Morra (also called Throwing fingers) • In your game the computer will pick either 1 or 2. • The computer then prompts the user to select either a 1 or a 2. • If the result is even the computer wins. If the result is odd the user wins. The computer needs to tell the result. • You have today and tomorrow to complete this program.

More Related