28
ECS 10 ECS 10 10/8 10/8

ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

Embed Size (px)

Citation preview

Page 1: ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

ECS 10ECS 10

10/810/8

Page 2: ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

Outline

Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding

sys.exit(…) Example: Coin flipping (if time

permits)

Page 3: ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

Announcements

Professor Amenta will be back on Friday

Dan Alcantara will be covering Monday and Wednesday lectures E-mail address: [email protected]

Slides will be on class website Temporary location: http://idav.ucdavis.edu/~dfalcant/ecs10/10-08-07.ppt

Page 4: ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

Homework 2

Homework 2 due Wednesday at 10PM

Don’t use sys.exit(…) Will explain why later today

Questions?

Page 5: ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

Boolean expressions

if name == “” :name = “Anonymous”print “We’ll call you”,

name, “!”

Boolean Boolean expressionexpression

Page 6: ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

Boolean algebraBoolean algebra Named after George Named after George

Boole (1815-1864)Boole (1815-1864) Main idea: you can Main idea: you can

write down logic as write down logic as mathematical formulas, mathematical formulas, as well as in sentences. as well as in sentences.

Logic as a Logic as a computational system. computational system. Python does some of Python does some of this computation!this computation!

Page 7: ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

BooleanBoolean expressions expressions

BooleanBoolean is a new data type. 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 We now have four data types (and

four kinds of expressions): four kinds of expressions):

Data type Examples

Integer 1, 10, 53

Floating point 3.14159, 6.29

String “Hey”, “”, “String”

Boolean True, False

Page 8: ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

Basic Boolean expressions

Expression Expression in EnglishX == 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.

Page 9: ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

Boolean expression examples

Boolean expressionBoolean expression English expressionEnglish expression ValueValue

““Something” == Something” == “Something”“Something”

The string “Something” The string “Something” is exactly the same as is exactly the same as

the string “Something”.the string “Something”.

TrueTrue

3 < 53 < 5 The integer 3 is less The integer 3 is less than 5.than 5.

TrueTrue

3 > 53 > 5 The integer 3 is greater The integer 3 is greater than 5.than 5.

FalseFalse

““CCat” == “at” == “ccat”at” The string “Cat” is The string “Cat” is exactly the same as the exactly the same as the

string “cat”.string “cat”.

FalseFalse

““Cat” != “cat”Cat” != “cat” The string “Cat” is The string “Cat” is different from the different from the

string “cat”.string “cat”.

TrueTrue

Page 10: ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

Compound Boolean expressions

Can combine multiple Boolean expressions with ‘and’ and ‘or’ keywords.

Boolean expression

0 < 10 and 0 < 5

English expression

0 is less than 10 and 0 is less than 5

Value TrueBoolean expression

“Dog” == “Dog” or 3 + 2 > 5

English expression

“Dog” is exactly the same as “Dog” or 3+2 is greater than 5

Value TrueBoolean expression

2 + 2 == 4 and 1 + 1 == 11

English expression

2+2 equals 4 and 1 + 1 equals 11

Value False

Page 11: ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

The if statement The if statement dissecteddissected

‘If’ statements change the flow of the program

Allows different things to happen under different conditions

Page 12: ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

The if statement The if statement dissecteddissected

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

Page 13: ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

The if statement The if statement dissecteddissected

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 name Player gave a name

Welcome the player

Page 14: ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

The if statement The if statement dissecteddissected

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 name Player gave a name

Page 15: ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

The if statement The if statement dissecteddissected

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.

Page 16: ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

The if statement The if statement dissecteddissected

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.

Page 17: ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

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.

The if statement The if statement dissecteddissected

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.

Page 18: ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

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

Page 19: ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

Why sys.exit(…) is badplayerAnswer = 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”

Page 20: ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

Why sys.exit(…) is badplayerAnswer = 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”

Page 21: ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

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!”

Page 22: ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

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

Page 23: ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

Our first moduleOur first module A A modulemodule is a collection is a collection

of Python functions, of Python functions, maybe other stuff thrown maybe other stuff thrown in (library in other in (library in other computer languages) computer languages)

import import the module to the module to use its functionsuse its functions

For random numbers, For random numbers, need the need the randomrandom module module

All the functions in All the functions in module module randomrandom are are named named randomrandom.something().something()

Your program

‘randomrandom’ module: Generates random numbers

import random

Page 24: ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

# get access to random number moduleimport random# use function that picks a random # integer between 1 and 10 (inclusive)x = random.randint(1,10)

Random moduleRandom module Program that picks a number between 1 and 10Program that picks a number between 1 and 10

Page 25: ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

Example program: Coin Coin flippingflipping

Use random.randint(1,2) to pick Use random.randint(1,2) to pick either 1 or 2either 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.

Page 26: ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

Start simple, then…Start simple, then…

Now let user pick Heads or TailsNow let user pick Heads or Tails User’s choice has to match coin.User’s choice has to match coin. Uses Boolean Uses Boolean andand and and oror. .

Page 27: ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

Key commandKey command

if (choice == "H" and coin == 1)\ or (choice == "T" and coin == 2):

\ means statement continues on next \ means statement continues on next lineline

Two ways to win; if either situation Two ways to win; if either situation occurs, user wins. occurs, user wins.

Each situation is an Each situation is an andand ““or” or” them togetherthem together

Page 28: ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin

Use to check inputUse to check input

Or:Or:

And:And:

if (choice == "H") or (choice == "T"):

if (choice != "H") and (choice != "T"):