eecs 110: lec 11: indefinite loops and program design aleksandar kuzmanovic northwestern university...

53
EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University http://cs.northwestern.edu/~akuzma/classes/ EECS110-s09/

Upload: leon-long

Post on 18-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

EECS 110: Lec 11: Indefinite Loops and Program

Design

Aleksandar Kuzmanovic

Northwestern University

http://cs.northwestern.edu/~akuzma/classes/EECS110-s09/

Page 2: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Final Exam

• When:– Wednesday 6/3/09

• Where:– Ann. Hall G15

• Time:– 10AM – 11:30AM

Page 3: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Input and typing trouble!

print 'Please input a number of meters'

meters = raw_input() # get input from usercm = meters * 100 # convert to centimeters

print 'That is', cm, 'cm.' # print out the result

Page 4: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Input and typing trouble!

print 'Please input a number of meters'

meters = raw_input() # get input from usercm = meters * 100 # convert to centimeters

print 'That is', cm, 'cm.' # print out the result

>>>Please input a number of meters5That is 5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555 cm.

What is python thinking ?!?

Page 5: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Fix #1: use a type converter

print 'Please input a number of meters'

meters = int(raw_input()) cm = meters * 100

print 'That is', cm, 'cm.'

check out my newly installed int converter!

The type of variable (box) matters!name: meters

type: intname: cmtype: int

1 100

Page 6: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Fix #1: use a type converter

print 'Please input a number of meters'

meters = int(raw_input()) # get int input from usercm = meters * 100

print 'That is', cm, 'cm.'

print 'Please input a number of meters'

meters = float(raw_input()) # get float input from usercm = meters * 100

print 'That is', cm, 'cm.'

str converts to string type

Page 7: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Fix #2: use input()

print 'Please input a number of meters'

meters = input() # get processed input from usercm = meters * 100

print 'That is', cm, 'cm.'

Why not ALWAYS use input() ?

Page 8: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Fix #2: use input()

print 'Please input a number of meters'

meters = input() # get processed input from usercm = meters * 100

print 'That is', cm, 'cm.'

Why not ALWAYS use input() ?

raw_input always returns input as a string!

input processes the input as if typed into Python

both allow you to input a prompt string

Page 9: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

input vs. raw_input

reply = raw_input('Enter a string and I\'ll tell you what I see.')

for c in reply:

print 'I see a(n) ', c

interprets what the user types as a string of characters

reply = input('Enter any list and I\'ll tell you what I see.')

for c in reply:

print 'I see a(n) ', c

processes what the user types just as python would

Page 10: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

More with Loopy thinking

s ='gattacaaggtaaaatgca'0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

How could we find the longest sequence of 'a's ?

How could we find the number of 'a's ? How about 'a's or 't's?How could we find the number of 'ta's ?

Page 11: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Loopy thinking

s ='gattacaaggtaaaatgca'0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

How could we find the longest sequence of 'a's ?

How could we find the number of 'a's ? How about 'a's or 't's?How could we find the number of 'ta's ?

Page 12: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Loopy thinking

s ='gattacaaggtaaaatgca'0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

How could we find the longest sequence of 'a's ?

How could we find the number of 'a's ? How about 'a's or 't's?How could we find the number of 'ta's ?

s = 'gattacaaggtaaaatgca' N = 0for i in range(0,len(s)): if s[i] == 'a': N = N + 1

print 'N is', N

Page 13: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Loopy thinking

s = 'gattacaaggtaaaatgca'0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

How could we find the longest sequence of 'a's ?

How could we find the number of 'a's ? How about 'a's or 't's?How could we find the number of 'ta's ?

Page 14: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Loopy thinking

s = 'gattacaaggtaaaatgca'0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

How could we find the longest sequence of 'a's ?

How could we find the number of 'a's ? How about 'a's or 't's?How could we find the number of 'ta's ?

s = 'gattacaaggtaaaatgca' N = 0for i in range(0,len(s)): if s[i] == 'a' or s[i] == 't': N = N + 1

print 'N is', N

Page 15: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Loopy thinking

s ='gattacaaggtaaaatgca'0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

How could we find the longest sequence of 'a's ?

How could we find the number of 'a's ? How about 'a's or 't's?How could we find the number of 'ta's ?

Page 16: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Loopy thinking

s ='gattacaaggtaaaatgca'0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

How could we find the longest sequence of 'a's ?

How could we find the number of 'a's ? How about 'a's or 't's?How could we find the number of 'ta's ?

s = 'gattacaaggtaaaatgca' N = 0for i in range(1,len(s)): if s[i] == 'a' and s[i-1] == 't': N = N + 1

print 'N is', N

Page 17: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Loopy thinking

s ='gattacaaggtaaaatgca'0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

How could we find the longest sequence of 'a's ?

How could we find the number of 'a's ? How about 'a's or 't's?How could we find the number of 'ta's ?

s = 'gattacaaggtaaaatgca' N = 0for i in range(1,len(s)): if s[i] == 'a' and s[i-1] == 't': N = N + 1

print 'N is', N

Page 18: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Loopy thinking

s ='gattacaaggtaaaatgca'0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

How could we find the longest sequence of 'a's ?

Page 19: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Loopy thinking

s ='gattacaaggtaaaatgca‘

- Len of current run- Len of best run

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

How could we find the longest sequence of 'a's ?

Page 20: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Planning in "pseudocode"

s ='gattacaaggtaaaatgca'0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

Loop through the string:

if we do see an 'a'

if the PREVIOUS letter is NOT an 'a'

if the PREVIOUS letter IS an 'a'

Keep track of CurRun, MaxRun

Page 21: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Planning in "pseudocode"

s ='gattacaaggtaaaatgca'0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

Loop through the string:

if we do see an 'a'

if the PREVIOUS letter is NOT an 'a'

if the PREVIOUS letter IS an 'a'

Keep track of CurRun, MaxRun

Start a Run! CurRun = 1

Continue our run! CurRun = CurRun + 1Check for a new maximum…

Page 22: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Planning in "pseudocode"

s ='gattacaaggtaaaatgca'0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

MAX = 0cur = 0for i in range(0,len(s)): if s[i] == 'a': if s[i-1] != 'a': cur = 1 else: cur = cur + 1 if cur > MAX: MAX = cur print 'Max is', MAX

Loop through the string:

if we do see an 'a'

if the PREVIOUS letter is NOT an 'a'

if the PREVIOUS letter IS an 'a'

Keep track of CurRun, MaxRun

Start a Run!

Continue our run!

Check for a new maximum…

Page 23: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

s = 'gattacaaggtaaaatgca' N = 0for i in range(0,len(s)): if s[i] == 'a': N = N + 1print 'N is', N

s = 'gattacaaggtaaaatgca' N = 0for i in range(1,len(s)): if s[i] == 'a' and s[i-1] == 't': N = N + 1print 'N is', N

s = 'gattacaaggtaaaatgca' MAX = 0cur = 0for i in range(0,len(s)): if s[i] == 'a': if s[i-1] != 'a': cur = 1 else: cur = cur + 1 if cur > MAX: MAX = cur print 'Max is', MAX

s = 'gattacaaggtaaaatgca' N = 0for i in range(0,len(s)): if s[i] == 'a' or s[i] == 't': N = N + 1print 'N is', N

1

2

3

4

Summary

Page 24: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Loops

for while

definite iteration

indefinite iteration

For a known number of iterations

For an unknown number of iterations

Page 25: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

def menu(): choice = 1 # Anything except 9 while choice != 9: print "I see danger in your future..." printMenu() choice = input("Make your choice ") print "The inner eye can see no more"

def printMenu(): print "What would you like to do?" print "\t 0: See another prediction" print "\t 9: Quit"

Seeing into the future…

"\t " represents a tab

Page 26: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

def menu(): while True: print "I see danger in your future..." printMenu() choice = input("Make your choice ") if choice == 9: break print "The inner eye can see no more"

def printMenu(): print "What would you like to do?" print "\t 0: See another prediction" print "\t 9: Quit"

Gimme a break

break stops the execution of the current loop

I'll figure out later how to get out of this loop!

OK – I'll stop the loop now and continue with the code after the loop

Page 27: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Indefinite Loops + input

A problem this week ask you to repeatedly interact with the user (TTSecurities)

What would you like to do? 0: Enter a new list 1: Print the current list 2: Find the average 9: Quit

Please enter your choice

The user's choice controls what happens

Page 28: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

def menu(): choice = 1 while choice != 9: printMenu() choice = input("Please enter your choice ") if choice == 0: print "You chose to enter a new list" elif choice == 1: print "You chose to print the list" elif choice == 2: print "You chose to find this average of the list" elif choice != 9: print "You made an invalid choice" print "Goodbye!"

def printMenu(): print "What would you like to do?" print "\t 0: Enter a new list" print "\t 1: Print the current list" print "\t 2: Find the average" print "\t 9: Quit"

Indefinite Loops + input

Page 29: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

if vs elif

def menu(): choice = 1 while choice != 9: printMenu() choice = input("Please enter your choice ") if choice == 0: print "You chose to enter a new list" if choice == 1: print "You chose to print the list" if choice == 2: print "You chose to find this average of the list" if choice != 9: print "You made an invalid choice" print "Goodbye!"

What happens if we change the elifs to ifs?

Page 30: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Indefinite Loops + input

def menu(): choice = 1 L = [] while choice != 9: printMenu() choice = input("Please enter your choice ") if choice == 0: L = getNewList() elif choice == 1: printList(L) elif choice == 2: averageList(L) elif choice != 9: print "You made an invalid choice" print "Goodbye!"

def getNewList(): print "You chose to get a new list" return []

Making a function for each choice makes the codecleaner and easier to modify

Page 31: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Gimme another break

def menu(): choice = 1 L = [] while True: printMenu() choice = input("Please enter your choice ") if choice == 0: L = getNewList() elif choice == 1: printList(L) elif choice == 2: averageList(L) elif choice == 9: break else: print "You made an invalid choice" print "Goodbye!"

Page 32: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

The Price Is Right!

Goal: Buy from a set of 5items (as many of each as you want)while spending between $9.25and $10.00.

Page 33: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Step 1: Identify Information To Store

• What information does this program need to keep track of?

Page 34: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Step 1: Identify Information To Store

• How much has the user spent? float: money

• Which items are available? list of strings: items

• How much does each available item cost? list of floats: prices

• Which item did the user currently choose?int: choice

• How many of the currently chosen item does the user want? int: number

Page 35: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Step 1: Identify Information To Store

• How much has the user spent? float: money

• Which items are available? [“coke”, “ramer”] list of strings: items

• How much does each available item cost? list of floats: prices

• Which item did the user currently choose?int: choice

• How many of the currently chosen item does the user want? int: number

Page 36: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Step 1: Identify Information To Store

• How much has the user spent? float: money

• Which items are available? [“coke”, “ramer”] list of strings: items

• How much does each available item cost? list of floats: prices [ 0.75, 0.25]

• Which item did the user currently choose?int: choice

• How many of the currently chosen item does the user want? int: number

Page 37: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Step 2: Break Down Functionality

• What are the things that this program needs to do?

Page 38: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Step 2: Break Down Functionality

• Control the overall game play

• Prompt the user to enter a choice and number of items

• Calculate the total spent

• Figure out when the game is over

• Figure out win or lose

• Print a welcome message

• Remove the purchased item from the lists (items, prices)

Page 39: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Attempt 1def playTry1(): print "Welcome to the price is right!" print "Your goal is to buy 5 or fewer items (any number of each)" print "and spend between $9.25 and $10" items = ["bleach","coke","ramen","ice cream","super ball"] prices = [1.35, .75, .25, 3.00, 1.75] money = 0.0 while money < 9.25 and len(items) > 0: print "You have spent $", money printItems( items ) choice = input( "Which item would you like to buy? ") number = input( "How many "+items[choice]+" would you like?") print items[choice], "is $", prices[choice] money += prices[choice]*number prices = prices[0:choice] + prices[choice+1:] items = items[0:choice] + items[choice+1:] print "You have spent $", money if money >= 9.25 and money <= 10.0: print "You win!" else: print "You lose!"

Page 40: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

"Quiz" part 1: Print Items

>>> items = [“coke”, “pepsi”, “sprite”]>>> printItems(items)0 : coke1 : pepsi2 : sprite

Page 41: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Attempt 1def playTry1(): print "Welcome to the price is right!" print "Your goal is to buy 5 or fewer items (any number of each)" print "and spend between $9.25 and $10" items = ["bleach","coke","ramen","ice cream","super ball"] prices = [1.35, .75, .25, 3.00, 1.75] money = 0.0 while money < 9.25 and len(items) > 0: print "You have spent $", money printItems( items ) choice = input( "Which item would you like to buy? ") number = input( "How many "+items[choice]+" would you like?") print items[choice], "is $", prices[choice] money += prices[choice]*number prices = prices[0:choice] + prices[choice+1:] items = items[0:choice] + items[choice+1:] print "You have spent $", money if money >= 9.25 and money <= 10.0: print "You win!" else: print "You lose!"

Page 42: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Attempt 1: Issuesdef playTry1(): print "Welcome to the price is right!" print "Your goal is to buy 5 or fewer items (any number of each)" print "and spend between $9.25 and $10" items = ["bleach","coke","ramen","ice cream","super ball"] prices = [1.35, .75, .25, 3.00, 1.75] money = 0.0 while money < 9.25 and len(items) > 0: print "You have spent $", money printItems( items ) choice = input( "Which item would you like to buy? ") number = input( "How many "+items[choice]+" would you like?") print items[choice], "is $", prices[choice] money += prices[choice]*number prices = prices[0:choice] + prices[choice+1:] items = items[0:choice] + items[choice+1:] print "You have spent $", money if money >= 9.25 and money <= 10.0: print "You win!" else: print "You lose!"

Magic numbers!

Page 43: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

Attempt 1adef playTry1a(): LIMIT_MIN = 9.25 LIMIT_MAX = 10.00 items = ["bleach","coke","ramen","ice cream","super ball"] prices = [1.35, .75, .25, 3.00, 1.75] money = 0.0 print "Welcome to the price is right!" print "Your goal is to buy", len(items), "or fewer items…" print "and spend between $", LIMIT_MIN, "and $", LIMIT_MAX while money < LIMIT_MIN and len(items) > 0: print "You have spent $", money printItems( items ) choice = input( "Which item would you like to buy? ") number = input( "How many "+items[choice]+" would you like?") print items[choice], "is $", prices[choice] money += prices[choice]*number prices = prices[0:choice] + prices[choice+1:] items = items[0:choice] + items[choice+1:] print "You have spent $", money if money >= LIMIT_MIN and money <= LIMIT_MAX: print "You win!" else: print "You lose!"

Page 44: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

def playTry1a(): LIMIT_MIN = 9.25 LIMIT_MAX = 10.00 items = ["bleach","coke","ramen","ice cream","super ball"] prices = [1.35, .75, .25, 3.00, 1.75] money = 0.0 print "Welcome to the price is right!" print "Your goal is to buy", len(items), "or fewer items…" print "and spend between $", LIMIT_MIN, "and $", LIMIT_MAX while money < LIMIT_MIN and len(items) > 0: print "You have spent $", money printItems( items ) choice = input( "Which item would you like to buy? ") number = input( "How many "+items[choice]+" would you like?") print items[choice], "is $", prices[choice] money += prices[choice]*number prices = prices[0:choice] + prices[choice+1:] items = items[0:choice] + items[choice+1:] print "You have spent $", money if money >= LIMIT_MIN and money <= LIMIT_MAX: print "You win!" else: print "You lose!"

Attempt 1a: Issue

Functionality is not broken out

Page 45: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

def playBetter(): """ Play the game """ [items, prices] = initilizeItems() LIMIT_HIGH = 10.00 LIMIT_LOW = 9.25 money = 0.0

printIntro(LIMIT_LOW, LIMIT_HIGH, items)

while not allDone(items, LIMIT_LOW, money): print "You have spent $", money [items, prices, spent] = playRound(items, prices) money += spent

winOrLose(money, LIMIT_LOW, LIMIT_HIGH)

Each function does one specific thing.It's clear where we go to change aspects of the game.

Code is self-commenting.

Functions can returnmore than one thingin a list

Page 46: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

def playRound(items, prices): """ Play one round of the game Inputs: A list of items and a list of prices Returns: [items, prices, spent] where items is list of items remaining, prices is a list of remaining prices, and spent is the amount spent this round""" print "*********************" printItems( items ) choice = input( "Which item would you like to buy? ") number = input( "How many " + items[choice] + " would you like? ") print items[choice], "is $", prices[choice]

spent = prices[choice]*number prices = prices[0:choice] + prices[choice+1:] items = items[0:choice] + items[choice+1:]

return [items, prices, spent]

Page 47: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

def playBetter(): """ Play the game """ [items, prices] = initilizeItems() LIMIT_HIGH = 10.00 LIMIT_LOW = 9.50 money = 0.0

printIntro(LIMIT_LOW, LIMIT_HIGH, items)

while not allDone(items, LIMIT_LOW, money): print "You have spent $", money [items, prices, spent] = playRound(items, prices) money += spent

winOrLose(money, LIMIT_LOW, LIMIT_HIGH)

You CAN (and should) modify your code as you goto make it cleaner and better organized.

(That is, you don’t have to get it perfect the first time)

Page 48: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

>>> diff([7, 0, 6, 4])

1

"Quiz"Print a list of strings with

the specified format

def diff( L ):

Return the min difference between any 2 elements in L

You need determine each element's index…

Example:

Example:

You can assume at least 2 elements in the list

Only consider unsigned differences. >>> items = [“coke”, “pepsi”, “sprite”]

>>> printItems(items)0 : coke1 : pepsi2 : sprite

def printItems( items ):

Name(s):_______________________________

Hint: use a NESTED loop

Page 49: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

"Quiz" part 1: Print Items

>>> items = [“coke”, “pepsi”, “sprite”]>>> printItems(items)0 : coke1 : pepsi2 : sprite

Page 50: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

"Quiz" part 1: Print Items

>>> items = [“coke”, “pepsi”, “sprite”]>>> printItems(items)0 : coke1 : pepsi2 : sprite

def printItems(items): for x in range(len(items)): print x,”:”,items[x]

Page 51: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

"Quiz" part 1: Print Items

>>> items = [“coke”, “pepsi”, “sprite”]>>> printItems(items)0:coke1:pepsi2:sprite

def printItems(items): for x in range(len(items)): print str(x)+”:”+items[x]

Page 52: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

>>> diff([7, 0, 6, 4])

1

"Quiz" Part 2

def diff( L ):

Return the min difference between any 2 elements in L

Example:

You can assume at least 2 elements in the list

Only consider unsigned differences.

Hint: use a NESTED loop

Page 53: EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University akuzma/classes/EECS110-s09

>>> diff([7, 0, 6, 4])

1

"Quiz" Part 2

def diff( L ):

mindif = abs(L[1] – L[0])

for i in range(len(L)-1):

for j in range(i+1,len(L)):

d = abs(L[i]-L[j])

if d < mindiff:

mindiff = d

return mindif

Return the min difference between any 2 elements in L

Example:

You can assume at least 2 elements in the list

Only consider unsigned differences.

Hint: use a NESTED loop