This preview shows page 1. Sign up to view the full content.
Unformatted text preview: 10/17/2007 Announcements
Midterm Mon Oct 22 ECS10
10/17 Bring any books or notes you want Bring a Scantron 2000 (buy at bookstore) Bring pencil Brin a p n il Know what section you are in (1-5) (1Practice test on Web page tomorrow New TA, Shri Alagesan
Taking over Section 1 (Fri 8AM) More Announcements
Talking in class is bothering other students. Tutoring is available. See course Web page for application form. UC D i unfortunately d Davis f l does not pay f private for i tutoring sessions, and your TAs do not give free private tutoring sessions. Rock, paper, scissors
Play the game with your neighbor. Example of a program structured like this: playing = True while playing: ..... if ...: # Tie else: playing = False ..... Equivalent Boolean Expressions
Expression not (u == "r" or u == "p" or u == "s") is True except when u is "r", "s",or "p" "s",or So is the expression u != "r" and u != "p" and u != "s" Either one can be used. There are many other examples of pairs of equivalent Boolean expressions. More than two cases...
Need to do something if rock, something else if scissors, and something else if paper.. if...elif...else if...elif...elif...elif...else if lif lif lif l 1 10/17/2007 if not (user == "r" or user == "s" or user == "p"): print "Not an allowed choice!" else: # Choose a random number between 1 and 2 number = random.randint(1,3) # Random number determines the program's choice if number == 1: program = "r" # 1 means rock elif number == 2: program = "p" # 2 means paper else: # number == 3: program = "s" # 3 means scissors Complicated logic
How to decide who won rock, paper, scissors? Make a written plan describing all possible cases Make sure your program handles all of them User R R R P P P S S S Prog R P S R P S R P S Winner Tie Prog User User Tie Prog Prog User Tie Easy approach
Could be one big if-elif-elif-....-elif-else if-elif-elif-....-elifExample: see rps-1.py rpsUse your plan to make sure you cover all cases! More slick...rps-2.py slick...rpsTies are easy to recognize if user == computer: computer:
print "We picked the same! Play again." Handle remaining cases in pairs
by user's choice, or by computer's choice, or by pair of symbols Use your plan to make sure you handle all cases! User R R R P P P S S S Prog R P S R P S R P S Winner Tie Prog User User Tie Prog Prog User Tie 2 10/17/2007 Keep playing
How to keep playing on bad input or when user and computer choose the same? Use a while loop. Structure to keep playing
playing = True while playing: ..... if ...: # Tie else: playing = False ..... In rps program
Boolean state variable playing True until somebody wins Set it to False when we get two different values, g , and somebody wins. 3 ...
View Full Document
This note was uploaded on 04/14/2008 for the course ECS 10 taught by Professor Amenta during the Fall '07 term at UC Davis.
- Fall '07