# eecs 110: lec 10: definite loops and user input

Post on 12-Jan-2016

20 views

Category:

## Documents

Embed Size (px)

DESCRIPTION

EECS 110: Lec 10: Definite Loops and User Input. Aleksandar Kuzmanovic Northwestern University. http://cs.northwestern.edu/~akuzma/classes/EECS110-s09/. Loops !. We've seen variables change in-place before:. [ x*6 for x in range(8) ]. [ 0, 6, 12, 18, 24, 30, 36, 42 ]. - PowerPoint PPT Presentation

TRANSCRIPT

• EECS 110: Lec 10: Definite Loops and User Input

Aleksandar KuzmanovicNorthwestern Universityhttp://cs.northwestern.edu/~akuzma/classes/EECS110-s09/

• Loops!We've seen variables change in-place before:[ x*6 for x in range(8) ][ 0, 6, 12, 18, 24, 30, 36, 42 ] remember range?

• fore!for x in range(8): print 'x is', x

print 'Phew!'x is assigned each value from this sequencethe BODY or BLOCK of the for loop runs with that xCode AFTER the loop will not run until the loop is finished.1234LOOP back to step 1 for EACH value in the list

• four on forfor x in range(8): print 'x is', xfactorial function?sum the list?construct the list?

• Accumulating an answerdef sum( L ): """ returns the sum of L's elements """ sum = 0

for x in L: sum = sum + x

return sum Finding the sum of a list:Accumulator!shortcuts?vs. recursion?sum every OTHER element?

• ShortcutShortcuts for changing variables:age = 38age = age + 1age += 1 #shortcut for age = age + 1

• Two kinds of for loopsElement-based Loopssum = 0

for x in L: sum += xL = [ 42, -10, 4 ]x"selfless"

• Two kinds of for loopsElement-based LoopsL = [ 42, -10, 4 ]xL = [ 42, -10, 4 ]i012Index-based Loopssum = 0

for x in L: sum += xsum = 0

for i in : sum +=

• Two kinds of for loopsElement-based LoopsL = [ 42, -10, 4 ]xL = [ 42, -10, 4 ]i012Index-based Loopssum = 0

for x in L: sum += xsum = 0

for i in range(len(L)): sum += L[i]L[i]

• Sum every other elementdef sum( L ): """ returns the sum of L's elements """ sum = 0

for i in range(len(L)): if ________:sum += L[i] return sum Finding the sum of a list:Accumulator!shortcuts?vs. recursion?sum every OTHER element?

• Sum every other elementdef sum( L ): """ returns the sum of L's elements """ sum = 0

for i in range(len(L)): if i%2 == 0:sum += L[i] return sum Finding the sum of a list:Accumulator!shortcuts?vs. recursion?sum every OTHER element?

• Extreme LoopingWhat does this code do?print 'It keeps on'

while True: print 'going and'

print 'Phew! I\'m done!'

• Extreme LoopingAnatomy of a while loop:print 'It keeps on'

while True: print 'going and'

print 'Phew! I\'m done!'

while loopthe loop keeps on running as long as this test is Truealternative tests?This won't print until the while loop finishes - in this case, never!

• Extreme Loopingimport time

print 'It keeps on'

while True: print 'going and' time.sleep(1)

print 'Phew! I\'m done!'while loopSlowing things downthe loop keeps on running as long as this test is True

• Making our escape!import randomescape = 0

while escape != 42: print 'Help! Let me out!' escape = random.choice([41,42,43])

print 'At last!'how could we count the number of loops we run?

• Loops aren't just for listsfor c in 'down with CS!': print c

• "Quiz"What do these two loops print?n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1print ndef min( L ):Write a loop to find and return the min of a list, LL is a list of numbers.n = 3while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1def isPrime( n ):Names:Write a loop so that this function returns True if its input is prime and False otherwise:n is a positive integer

• What do these two loops print?n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1print n

??n = 3while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1

• What do these two loops print?n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1print n

7n = 3while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1

??

• What do these two loops print?n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1print n

7n = 3while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1

3

• What do these two loops print?n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1print n

7n = 3while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1

310

• What do these two loops print?n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1print n

7n = 3while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1

3105

• What do these two loops print?n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1print n

7n = 3while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1

310516

• What do these two loops print?n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1print n

7n = 3while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1

3105168

• What do these two loops print?n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1print n

7n = 3while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1

31051684

• What do these two loops print?n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1print n

7n = 3while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1

310516842

• def min( L ):L is a list of numbers.def isPrime( n ):n is a positive integer

• L is a list of numbers.def isPrime( n ):n is a positive integerdef min( L ): mn = L[0] for i in range(1,len(L)): if L[i] < mn: mn = L[i] return mn

• L is a list of numbers.def isPrime( n ):n is a positive integerdef min( L ): mn = L[0] for i in range(1,len(L)): if L[i] < mn: mn = L[i] return mn

def min( L ): mn=L[0] for s in L: if s < mn: mn = s return mn

• def min( L ): mn = L[0] for i in range(1,len(L)): if L[i] < mn: mn = L[i] return mn

def min( L ): mn=L[0] for s in L: if s < mn: mn = s return mnL is a list of numbers.def isPrime( n ): for i in range (n): if i not in [0,1]: if n%i == 0: return False return True

n is a positive integer

• Consider the following update rule for all complex numbers c:z0 = 0zn+1 = zn2 + cIf z does not diverge, c is in the M. Set.Real axisImaginary axiscLab 8: the Mandelbrot SetBenoit M.z0z1z2z3z4

• Consider the following update rule for all complex numbers c:z0 = 0zn+1 = zn2 + cIf c does not diverge, it's in the M. Set.Real axisImaginary axiscLab 8: the Mandelbrot SetBenoit M.z0z1z2z3z4example of a non-diverging cycle

• Consider the following update rule for all complex numbers c:z0 = 0zn+1 = zn2 + c

Lab 8: the Mandelbrot SetThe shaded area are points that do not diverge.

• Python and imagesfor creating and saving imagesimport bmp.pyimage = BitMap( 300, 200, Color.GREEN )creates a bitmap objectand names it image

• Python and imagesfor creating and saving imagesimport bmp.pyimage = BitMap( 300, 200, Color.GREEN )creates a bitmap objectand names it imageobjects are software abstractions containing structured information

• Python and images and objectsimport bmp.pyimage = BitMap( 300, 200, Color.GREEN )here, a bitmap object named image is calling an internal method named saveFileobjects are variables that can contain their own functions, often called methodsimage.saveFile( "test.bmp" )

• Python and images and objectsimport bmp.pyimage = BitMap( 300, 200, Color.GREEN )two more internal methodsobjects are variables that can contain their own functions, often called methodsimage.saveFile( "test.bmp" )image.plotPixel( 150, 100 )image.setPenColor( Color.Red )

• Q: What does this plot?from bmp import *

def test(): """ image demonstration """ width = 200 height = 200 image = BitMap( width, height )

# a 2d loop over each pixel for col in range(width):

for row in range(height):

if col == row:

image.plotPoint( col, row )

image.saveFile( "test.bmp" )

• Q: What does this plot?A: A diagonal in the SW -> NE directionfrom bmp import *

def test(): """ image demonstration """ width = 200 height = 200 image = BitMap( width, height )

# a 2d loop over each pixel for col in range(width):

for row in range(height):

if col == row:

image.plotPoint( col, row )

image.saveFile( "test.bmp" )

• How could you change this code so that it plots a diagonal from NW to SE?def test(): """ demonstrating images """ width = 200 height = 200 image = BitMap( width, height )

# a 2d loop over each pixel for col in range(width):

for row in range(height):

if col == row:

image.plotPoint( col, row )

image.saveFile( "test.bmp" )

• How could you change this code so that it plots a diagonal from NW to SE?def test(): """ demonstrating images """ width = 200 height = 200 image = BitMap( width, height )

# a 2d loop over each pixel for col in range(width):

for row in range(height):

if col == height row -1:

image.plotPoint( col, row )

image.saveFile( "test.bmp" )

Recommended