[Tutor] class method problem

Joel Goldstick joel.goldstick at gmail.com
Mon Sep 27 16:37:52 CEST 2010

I looked up the word zoeken -- it means find in English.

I looked up the chapter and exercise in the online text "How to think
like a computer scientist"

Its a good tutorial.

I think the OP seems to get confused on basic concepts.

Here is my stab at the exercise:

#!/usr/bin/env python

"""This exercise is to take the find function, alter it so that it can
specify an end point for the search.  It is problem 2 at the end of this

The author notes that end should default to len(str), but it can't since
default parameters are initialized when the function is defined and not
when it is invoked.

# this is from the author's text
def find(str, ch, start=0):
    index = start
    while index < len(str):
        if str[index] == ch:
            return index
        index = index + 1
    return -1

# this I believe is the concept the author was looking to get the
reader to understand

def find2(str, ch, start=0, end = None):
    if end == None:
        end = len(str)
    index = start
    while index < end:
        if str[index] == ch:
            return index
        index = index + 1
    return -1

Joel Goldstick

More information about the Tutor mailing list