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