Docsity

Stopping the Program, Sentry Variable and Random Modules | ECS 010, Study notes of Computer Science

Toggle sidebar
bg1
10/15/2008
1
ECS10ECS10
10/1510/15
AnnouncementsAnnouncements
Midterm Mon Oct 20Midterm Mon Oct 20
Bring any books or notes you wantBring any books or notes you want
Bring a Bring a ScantronScantron 2000 (buy at bookstore)2000 (buy at bookstore)
Brin a encilBrin a encil
Brin
g
a
p
encilBrin
g
a
p
encil
Know what section you are in (1Know what section you are in (1--5)5)
Study! Study!
Stopping the program Stopping the program
Instead of looping on bad input, just stop.Instead of looping on bad input, just stop.
The best way in Python: use an The best way in Python: use an if if statementstatement
Introduce a Introduce a sentry variable sentry variable to keep track of to keep track of
hh d hh d
w
h
et
h
er
we
got
goo
d
input
or
not.
w
h
et
h
er
we
got
goo
d
input
or
not.
Sentry variableSentry variable
goodInput = True
print ('Enter your height.')
reply = raw_input('Feet: ')
if helper.isInt(reply):
feet = int(reply)
else:
goodInput = False
goodInputgoodInput is a Boolean variable; only do is a Boolean variable; only do
calculation if it is calculation if it is TrueTrue..
f
dI
Using the sentryUsing the sentry
i
f
goo
dI
nput:
inches = (feet*12) + inches
bmi = (pounds*703) / (inches*inches)
Rock, paper, scissorsRock, paper, scissors
Play the game with your neighbor. Play the game with your neighbor.
Example of a program structured like this:Example of a program structured like this:
tie= True
w
hile tie:
…..
if …: # Somebody wins
tie= False
…..
pf2

Partial preview of the text

Download Stopping the Program, Sentry Variable and Random Modules | ECS 010 and more Study notes Computer Science in PDF only on Docsity!

ECS10ECS

AnnouncementsAnnouncements

„„ Midterm Mon Oct 20Midterm Mon Oct 20 „„ Bring any books or notes you wantBring any books or notes you want „„ Bring aBring a ScantronScantron 2000 (buy at bookstore)2000 (buy at bookstore) „„„„ BrinBrinBring a pencilBring a pencil aa encilencil „„ Know what section you are in (1Know what section you are in (1--5)5) „„ Study!Study!

Stopping the programStopping the program

„„ Instead of looping on bad input, just stop.Instead of looping on bad input, just stop. „„ The best way in Python: use anThe best way in Python: use an ifif statementstatement „„ Introduce aIntroduce a sentry variablesentry variable to keep track ofto keep track of whether we got good input or not.whether we got good input or not.hh hh dd

Sentry variableSentry variable

goodInput = True print ('Enter your height.') reply = raw_input('Feet: ') if helper.isInt(reply): feet = int(reply) else: goodInput = False

„„ goodInputgoodInput is a Boolean variable; only dois a Boolean variable; only do calculation if it iscalculation if it is TrueTrue ..

f dI

Using the sentryUsing the sentry

if goodInput: inches = (feet12) + inches bmi = (pounds703) / (inches*inches)

Rock, paper, scissorsRock, paper, scissors

„„ Play the game with your neighbor.Play the game with your neighbor. „„ Example of a program structured like this:Example of a program structured like this: tie= True while tie: ….. if …: # Somebody wins

tie= False …..

The random moduleThe random module

import random

„„ Gives us functions that compute differentGives us functions that compute different kkkinds of random numbers.kinds of random numbers. dd ff dd bb

random.randrange(3)

„„ Produces a random choice of {0,1,2}Produces a random choice of {0,1,2}

Play until somebody winsPlay until somebody wins

„„ Use a sentry variable calledUse a sentry variable called tietie .. „„ Assume it’s a tie at the beginning.Assume it’s a tie at the beginning. „„ Play until it’s not a tie any more.Play until it’s not a tie any more.

Who wins?Who wins?

„„ Need to do something if rock, something elseNeed to do something if rock, something else if scissors, and something else if paper..if scissors, and something else if paper.. „„ if…if…elifelif…else…else „„ if…if…elifff elif…l fl f …elifelifl fl f (d(d(don’t need an else)(don’t need an else) ’’ dd ll ))

Let your program speak!Let your program speak!

„„ It would be nicer if your program could talkIt would be nicer if your program could talk about ‘rock’ instead of ‘r’.about ‘rock’ instead of ‘r’. „„ Use a list of words likeUse a list of words like words=[‘words=[‘words=[‘words=[‘rock’ ’paper’ ’scissorsrock’ ’paper’ ’scissorsrock’rock’,’paper’,’scissors’].,’paper’,’scissors’]’]’]. „„ Python lets you refer to the things in a list byPython lets you refer to the things in a list by their position in the list. For example:their position in the list. For example: words[0]words[0] # This has the value ‘rock’# This has the value ‘rock’