Download presentation
Presentation is loading. Please wait.
Published byChristian Stafford Modified over 9 years ago
1
ECS 10 10/8
2
Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin flipping (if time permits)
3
Announcements Professor Amenta will be back on Friday Dan Alcantara will be covering Monday and Wednesday lectures E-mail address: dfalcantara@gmail.comdfalcantara@gmail.com Slides will be on class website Temporary location: http://idav.ucdavis.edu/~dfalcant/ecs10/10-08-07.ppt http://idav.ucdavis.edu/~dfalcant/ecs10/10-08-07.ppt
4
Homework 2 Homework 2 due Wednesday at 10PM Don’t use sys.exit(…) Will explain why later today Questions?
5
Boolean expressions if name == “” : name = “Anonymous” print “We’ll call you”, name, “!” Boolean expression
6
Boolean algebra Named after George Boole (1815-1864) Named after George Boole (1815-1864) Main idea: you can write down logic as mathematical formulas, as well as in sentences. Main idea: you can write down logic as mathematical formulas, as well as in sentences. Logic as a computational system. Python does some of this computation! Logic as a computational system. Python does some of this computation!
7
Boolean expressions Boolean is a new data type. Boolean is a new data type. Booleans can be either True or False. Booleans can be either True or False. We now have four data types (and four kinds of expressions): We now have four data types (and four kinds of expressions): Data typeExamples Integer1, 10, 53 Floating point3.14159, 6.29 String“Hey”, “”, “String” BooleanTrue, False
8
Basic Boolean expressions ExpressionExpression in English X == Y“X is equal to Y” X != Y“X is not equal to Y” X < Y“X is less than Y” X > Y“X is greater than Y” X <= Y“X is less than or equal to Y” X >= Y“X is greater than or equal to Y” not ______“Whatever the condition, it’s not True” Boolean expressions are True if the expression in English is True, and False otherwise.
9
Boolean expression examples Boolean expression English expression Value “Something” == “Something” The string “Something” is exactly the same as the string “Something”. True 3 < 5 The integer 3 is less than 5. True 3 > 5 The integer 3 is greater than 5. False “Cat” == “cat” The string “Cat” is exactly the same as the string “cat”. False “Cat” != “cat” The string “Cat” is different from the string “cat”. True
10
Compound Boolean expressions Can combine multiple Boolean expressions with ‘and’ and ‘or’ keywords. Boolean expression0 < 10 and 0 < 5 English expression0 is less than 10 and 0 is less than 5 ValueTrue Boolean expression“Dog” == “Dog” or 3 + 2 > 5 English expression“Dog” is exactly the same as “Dog” or 3+2 is greater than 5 ValueTrue Boolean expression2 + 2 == 4 and 1 + 1 == 11 English expression2+2 equals 4 and 1 + 1 equals 11 ValueFalse
11
The if statement dissected ‘If’ statements change the flow of the program Allows different things to happen under different conditions
12
The if statement dissected if name == “” : name = “Anonymous” print “We’ll call you”, name, “!” else: print “Welcome,”, name, “!” If the player doesn’t have a name, then: Call the player “Anonymous” Tell the player that he/she will be called “Anonymous” If the player does have a name, then: Welcome the player
13
The if statement dissected if name == “” : name = “Anonymous” print “We’ll call you”, name, “!” else: print “Welcome,”, name, “!” Check the player’s name Continue the program Tell the player they’ve been renamed Rename the player “Anonymous” Player didn’t give a namePlayer gave a name Welcome the player
14
The if statement dissected if name == “” : name = “Anonymous” print “We’ll call you”, name, “!” Check the player’s name Continue the program Tell the player they’ve been renamed Rename the player “Anonymous” Player didn’t give a namePlayer gave a name
15
The if statement dissected if name == “” : name = “Anonymous” print “We’ll call you”, name, “!” ‘if’ is a Python command that checks whether a condition is True or False and changes what the program does next.
16
The if statement dissected if name == “” : name = “Anonymous” print “We’ll call you”, name, “!” ‘if’ is a Python command that checks whether a condition is True or False and changes what the program does next. This is a block, which consists of a sequential set of lines indented the same way. The entire block is run when the condition is True.
17
The if statement dissected if name == “” : name = “Anonymous” print “We’ll call you”, name, “!” ‘if’ is a Python command that checks whether a condition is True or False and changes what the program does next. This is a condition, which is either True or False. This specific condition checks to see whether or not the variable “name” equals the empty string.
18
Side note: Why sys.exit(…) is bad Some of you have been using it for program 2 Breaks the flow of the program Anything that is expected to happen after the sys.exit(…) call doesn’t happen
19
Why sys.exit(…) is bad playerAnswer = raw_input(“What’s your answer? ”) if playerAnswer == “42”: print “You’re right!” playerScore = playerScore + 1000 else: print “You got it wrong!” print “Your final score is”, playerScore Ask for the player’s answer Check the player’s answer Insult the player Print the final score Award the player 1000 points Congratulate the player Answered “42” Didn’t answer “42”
20
Why sys.exit(…) is bad playerAnswer = raw_input(“What’s your answer? ”) if playerAnswer == “42”: print “You’re right!” playerScore = playerScore + 1000 else: sys.exit(“You got it wrong!”) print “Your final score is”, playerScore X Ask for the player’s answer Check the player’s answer Insult the player Print the final score Award the player 1000 points Congratulate the player Answered “42” Didn’t answer “42”
21
Booleans as state variables State variables store information about the program’s current state For homework 2, can be combined with ‘if’ statements to determine if program should stop Their use avoids the need for sys.exit(…) call skipTheRules = True if skipTheRules == False: print “Rule 1” print “Rule 2” print “On with the game!”
22
Example program: Coin flipping Objective: Python program will simulate flipping a coin Coin will land on either heads or tails User will guess either heads or tails before the flip Problem: Python doesn’t have any way of randomly flipping a coin Need to add a module
23
Our first module A module is a collection of Python functions, maybe other stuff thrown in (library in other computer languages) A module is a collection of Python functions, maybe other stuff thrown in (library in other computer languages) import the module to use its functions import the module to use its functions For random numbers, need the random module For random numbers, need the random module All the functions in module random are named random.something() All the functions in module random are named random.something() Your program random ‘random’ module: Generates random numbers import random
24
# get access to random number module import random # use function that picks a random # integer between 1 and 10 (inclusive) x = random.randint(1,10) Random module Program that picks a number between 1 and 10 Program that picks a number between 1 and 10
25
Coin flipping Example program: Coin flipping Use random.randint(1,2) to pick either 1 or 2 Use random.randint(1,2) to pick either 1 or 2 If 1, then coin is heads. If 1, then coin is heads. If 2, then coin is tails. If 2, then coin is tails.
26
Start simple, then… Now let user pick Heads or Tails Now let user pick Heads or Tails User’s choice has to match coin. User’s choice has to match coin. Uses Boolean and and or. Uses Boolean and and or.
27
Key command if (choice == "H" and coin == 1)\ or (choice == "T" and coin == 2): \ means statement continues on next line \ means statement continues on next line Two ways to win; if either situation occurs, user wins. Two ways to win; if either situation occurs, user wins. Each situation is an and Each situation is an and “or” them together “or” them together
28
Use to check input Or: Or: And: And: if (choice == "H") or (choice == "T"): if (choice != "H") and (choice != "T"):
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.