[Tutor] A very simple question

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Thu, 30 May 2002 10:37:31 -0700 (PDT)


> I'd like to write a function that gets a list , checks if there are only
> strings in the list starting with a lowercase letter and then returns 1
> if only lowercase, 0 if not. How can I do that?

Hi Nicole,


Let's take a look at what you have:

> def checkIfOnlyLower(list):
>      for item in list:
>           if item[0].islower():
>              return 1
>          else:
>             return 0
>
> the function will return 1 as soon as it comes to the first string starting
> with a lowercase letter.But that's not what I want.


Very true, checkifOnlyLower() thinks that it knows the answer too quickly.
Take a closer look at the loop:

###
for item in list:
    if item[0].islower():
        return 1
    else:
        return 0
###


A simple approach of debugging a program is to "trace" it out --- that is,
go through the motions on a piece of paper and try simulating what the
computer will do.


Good luck to you!