Lecture 11: Iteration and For-Loops - Cornell …

[Pages:41]

Lecture 11: Iteration and For-Loops

(Sections 4.2 and 10.3) CS 1110

Introduction to Computing Using Python

[E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]

Announcements: Prelim 1

? Rooms:

? AA1 -- JKZ99999 Baker Laboratory 200

? JL1 -- SEZ99999 Rockefeller 201

? SF1 -- ZZ999999 Rockefeller 203

? covers material up through today

? What to study: A1, A2, Labs 1-6, old exam questions:

? Prelim study guide on webpage under "Exams"

? If you requested a makeup via CMS you will receive an emailed today about whether the request is granted.

? Email SDS letters to cs1110-prof@cornell.edu, and

Cc: Jenna Edwards jls478@cornell.edu

2

Announcements

? Don't skip lab! (especially not this lab!)

? A2 Due Thursday. Solutions released Friday ? A2 must be submitted as a PDF

? Convert your files to pdf with Genius Scan

3

More Announcements

? Lab 6: due in two weeks

? Tuesday 3/13 labs: open office hours ? Wednesday 3/14 labs: cancelled

? Thursday 3/8: optional in-class review session ? Tuesday 3/13: no lecture; office hours instead

? Olin 155 during class times, Olin 128 in between

? A3: released sometime after Prelim 1

4

Advice from Consultants about A1 Tests Cases

Test for:

? all possible input patterns

? all possible outputs you might expect

Remember:

? Space is not the same thing as the empty string

? When you are thinking of test cases, write

comments next to them explaining why you

think they are different from the other cases.

This will help you come up with distinct test

cases.

5

Problem: Summing the Elements of a List

def sum(the_list): """Returns: the sum of all elements in the_list Precondition: the_list is a list of all numbers (either floats or ints)"""

6

Approach: Summing the Elements of a List

def sum(the_list): """Returns: the sum of all elements in the_list Precondition: the_list is a list of all numbers (either floats or ints)""" # Create a variable to hold result (start at 0) # Add each list element to variable # Return the variable

How will we do this?

7

1st Attempt: Summing the Elements of a List

def sum(the_list):

"""Returns: the sum of all elements in the_list

Precondition: the_list is a list of all numbers (either floats or ints)"""

result = 0

result = result + the_list[0]

result = result + the_list[1]

...

Houston, we

return result have a problem

8

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download