Newbie append() question

Brian Blazer brian at brianandkate.com
Thu May 18 22:07:40 EDT 2006


I promise that this is not homework.  I am trying to self teach here  
and have run into an interesting problem.  I have created a method  
that asks for a class name and then is supposed to add it to classes 
[].  Here is a snippet:

def getCurrentClasses():
         classes = []
         print 'Please enter the class name. When finished enter D.'
         while (c != "D"):
             c = raw_input("Enter class name")
             if (c != "D"):
                 classes.append(c)

I have been running this in the interactive interpreter and if I  
print the list I get the string "Enter class name" as the first entry  
in the list and what was supposed to be the first entry as the second  
element like this:

Enter class name: cs1
['Enter class name: cs1']

I guess that I assumed that c would be equal to the value entered by  
the user not the prompt string.  It actually looks like it is taking  
the whole thing as one string.  But then if I enter more classes I  
get this:

Enter class name: cs2
['Enter class name: cs1', 'cs2']

So with the second and successive inputs, it appends the entered string.

Hopefully someone could enlighten me as to what is going on and maybe  
offer a suggestion to help me figure this one out.

Thank you for your time,

Brian
brian at brianandkate.com







More information about the Python-list mailing list