ecs10-10-15 - 10/15/2008 Announcements Midterm Midterm Mon...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 10/15/2008 Announcements Midterm Midterm Mon Oct 20 ECS10 10/15 Bring Bring any books or notes you want Bring Bring a Scantron 2000 (buy at bookstore) Brin Bring a pencil Know Know what section you are in (1-5) (1Study! Study! Stopping Stopping the program Instead Instead of looping on bad input, just stop. The The best way in Python: use an if statement if statement Introduce sentry Introduce a sentry variable to keep track of to whether we got good input or not. Sentry Sentry variable goodInput = True print ('Enter your height.') reply = raw_input('Feet: ') if helper.isInt(reply): feet = int(reply) else: goodInput = False Using the sentry goodInput goodInput is a Boolean variable; only do calculation if it is True. True if goodInput: dI inches = (feet*12) + inches bmi = (pounds*703) / (inches*inches) Rock, paper, scissors Play Play the game with your neighbor. Example Example of a program structured like this: tie= True while tie: ….. if …: # Somebody wins tie= False ….. 1 10/15/2008 The random module import random Gives Gives us functions that compute different kinds of random numbers. random.randrange(3) Produces Produces a random choice of {0,1,2} Play until somebody wins Use Use a sentry variable called tie. tie Assume Assume it’s a tie at the beginning. Play Play until it’s not a tie any more. Who Who wins? Need Need to do something if rock, something else if scissors, and something else if paper.. if…elif…else if…elif…else if…elif…elif (don’t need an else) …e Let your program speak! It It would be nicer if your program could talk about ‘rock’ instead of ‘r’. Use Use a list of words like words=[‘ words=[‘rock’,’paper’,’scissors’]. Python Python lets you refer to the things in a list by their position in the list. For example: words[0] # This has the value ‘rock’ This 2 ...
View Full Document

This note was uploaded on 03/23/2011 for the course ECS 10 taught by Professor Amenta during the Winter '07 term at UC Davis.

Ask a homework question - tutors are online