Re: Newbie append() question
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 -- The problem with the world is stupidity. Not saying there should be a capital punishment for stupidity, but why don't we just take the safety labels off of everything and let the problem solve itself? Frank Zappa -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie append() question
Thanks guys. Your solutions worked. I'm still not sure why it was grabbing the prompt string though. Thanks again, Brian [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie append() question
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 -- The problem with the world is stupidity. Not saying there should be a capital punishment for stupidity, but why don't we just take the safety labels off of everything and let the problem solve itself? Frank Zappa -- http://mail.python.org/mailman/listinfo/python-list
Newbie append() question
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 [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie append() question
Brian Blazer wrote: 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 [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list