Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 19 Online
OpenStudy (anonymous):

I can't figure out how to define a list as part of a program. The best I can do is create the empty list prior to making the program. Can anyone tell me how to do that? Here's what I mean: http://dpaste.com/hold/605253/ (it's assignment 2 problem 3)

OpenStudy (anonymous):

that's how you do it.

OpenStudy (anonymous):

yeah, you got it, there's nothing wrong or odd about creating an empty list. in fact if you call the list.append(x) method before you have a list, Python will raise an exception I think. you can also create a non empty list if you know what's going to go into it ahead of time: listName = [x, y, 'a', 'b', variableName, etc] also, it's not a good habit to capitalize the first letter of a variable or object, in some languages that will cause unwanted effects. lowercaseThenUpperCaseIfMoreThanOneWord enough rambling. enjoy!

OpenStudy (anonymous):

jesse.bo- what version of python are you using? When I use the "list.append(x) " function, I get an error in Python 2.7.2 saying that "append isn't valid.

OpenStudy (anonymous):

are you typing in 'list.append(x)' exactly, without substituting the 'list' part for the identifier of an actual list you need to use? a = ['hello'] a.append('mom') is totally valid

OpenStudy (anonymous):

yeah, sorry, when I said list.append, i meant to substitute 'list' for your list name. probably should have written listName.append(value or expression) I am using 2.7 and 3.1, but from what I hear, they have put most of the 3.x improvements into the 2.7 build, so you can use 3 syntax even if you are using 2.7, but not necessarily visa versa, i.e. you can't type: print 'hello World' #you can't type this in build 3.x print('hello world') would work in both 3 and 2.7. It was converted to a function in 3 but I digress.

OpenStudy (anonymous):

Doh! My problem was not with syntax. It turns out that Python doesn't know how to append a list with a non-existent value! Oops- I guess I need to figure out how to generate a value before I try to stick it into a list.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!