

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


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!
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
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
if goodInput: inches = (feet12) + inches bmi = (pounds703) / (inches*inches)
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’