Newbie append() question

B

Brian Blazer

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
(e-mail address removed)
 
G

Gerard Flanagan

Brian said:
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
(e-mail address removed)

your code gives me:

UnboundLocalError: local variable 'c' referenced before assignment

Is this the exact code you ran?

This should work:

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

but you are checking the same condition twice: c!= 'D', which is
unnecessary.Try:

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

getCurrentClasses2()

Gerard
 
S

Sybren Stuvel

Brian Blazer enlightened us with:
def getCurrentClasses():
classes = []
print 'Please enter the class name. When finished enter D.'
while (c != "D"):

No need for the parentheses, and 'c' doesn't have a value yet. If you
add 'c=""' before the while-loop, it should be fine.
c = raw_input("Enter class name")
if (c != "D"):

Here there is also no need for parentheses.

Sybren
 
B

Brian Blazer

Thanks guys. Your solutions worked.

I'm still not sure why it was grabbing the prompt string though.

Thanks again,

Brian
(e-mail address removed)
 
S

Sybren Stuvel

Brian Blazer enlightened us with:
I'm still not sure why it was grabbing the prompt string though.

Me neither. Try it in a standalone script instead of an interactive
session.

Sybren
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top