Re: [Tutor] List of ints
- Original Message - > From: Mark Lawrence > To: tutor@python.org > Cc: > Sent: Wednesday, March 4, 2015 10:20 AM > Subject: Re: [Tutor] List of ints > > On 04/03/2015 00:25, Steven D'Aprano wrote: >> On Tue, Mar 03, 2015 at 04:50:41PM +1000, Phil wrote: >> >>> count [0] += 1 >>> >>> This fails with the following error; >>> >>> TypeError: 'int' object is not iterable >> >> I know that others have already solved the problem, but here is >> something which might help you solve similar problems in the future. >> The way to debug simple things like this is quite simple: >> >> print count[0] >> >> which will show you that count[0] is a list [0], not an int 0, and you >> are trying to add [0]+1 which doesn't work. >> >> Never under-estimate the usefulness of a few print calls when debugging. >> >> > > About time we threw in the use of the interactive interpreter for trying > code snippets as well, just for good measure :) Perhaps both at the same time using the PDB debugger from within IPython? %run -d -b myscript.py d = debug, b = breakpoint inside pdb: c = continue, q = quit, p = print, r = return value of current function, s = step, and more: https://docs.python.org/2/library/pdb.html ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] List of ints
On 04/03/2015 00:25, Steven D'Aprano wrote: On Tue, Mar 03, 2015 at 04:50:41PM +1000, Phil wrote: count [0] += 1 This fails with the following error; TypeError: 'int' object is not iterable I know that others have already solved the problem, but here is something which might help you solve similar problems in the future. The way to debug simple things like this is quite simple: print count[0] which will show you that count[0] is a list [0], not an int 0, and you are trying to add [0]+1 which doesn't work. Never under-estimate the usefulness of a few print calls when debugging. About time we threw in the use of the interactive interpreter for trying code snippets as well, just for good measure :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] List of ints
On 03/03/15 23:44, Mark Lawrence wrote: Having never heard of QPython I've just looked it up, so for those who don't know from http://qpython.com/ it's "a script engine which runs Python programs on android devices". I doubt if there is much experience on this list with it although you might get lucky. I have QPython on my Android phone. It's not much good for development but its ok for running a few basic Python scripts. Obviously GUIs etc won't work but file processing and the like are fine. But I certainly wouldn't try programming in it! But then I don't do any programming on my phone anyway. On my tablet I use vim as editor and Qpython to run the code but its still not an experience I recommend. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] List of ints
On Tue, Mar 03, 2015 at 04:50:41PM +1000, Phil wrote: > count [0] += 1 > > This fails with the following error; > > TypeError: 'int' object is not iterable I know that others have already solved the problem, but here is something which might help you solve similar problems in the future. The way to debug simple things like this is quite simple: print count[0] which will show you that count[0] is a list [0], not an int 0, and you are trying to add [0]+1 which doesn't work. Never under-estimate the usefulness of a few print calls when debugging. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] List of ints
On Wed, Mar 04, 2015 at 09:09:03AM +1000, Phil wrote: > I'd been away from home for five weeks and during a quiet period I > installed QPython on my tablet with the aim of porting a programme that > I'd written in C++ 15 years ago to Python. Cutting and pasting and even > moving around the IDE turned out to be a truly frustrating exercise. I don't actually know QPython, but in general, using a tablet or a smart phone is only acceptable for the most trivial actions. Fine if you're taping out a 15 character tweet with one finger, not so useful if you want to get real work done. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] List of ints
On 03/03/2015 23:09, Phil wrote: On 03/03/15 17:46, Mark Lawrence wrote: You are trying to increment the first element of count which is itself a list containing one element. You actually need:- count[0][0] +=1 Thank you Lawrence, Alan, and Danny, The solution is embarrassingly obvious. It's been a long time since I've attempted any programming and I'd even forgotten that I needed a nested loop to access the cells in a two-dimensional array, or list. In this case I didn't need a two-dimensional array anyway. I'd been away from home for five weeks and during a quiet period I installed QPython on my tablet with the aim of porting a programme that I'd written in C++ 15 years ago to Python. Cutting and pasting and even moving around the IDE turned out to be a truly frustrating exercise. I wonder if it was just my clumsiness or if others have had the same experience? Having never heard of QPython I've just looked it up, so for those who don't know from http://qpython.com/ it's "a script engine which runs Python programs on android devices". I doubt if there is much experience on this list with it although you might get lucky. I am aware though of http://bugs.python.org/issue23496 "Steps for Android Native Build of Python 3.4.2" which may be of interest. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] List of ints
On 03/03/15 17:46, Mark Lawrence wrote: You are trying to increment the first element of count which is itself a list containing one element. You actually need:- count[0][0] +=1 Thank you Lawrence, Alan, and Danny, The solution is embarrassingly obvious. It's been a long time since I've attempted any programming and I'd even forgotten that I needed a nested loop to access the cells in a two-dimensional array, or list. In this case I didn't need a two-dimensional array anyway. I'd been away from home for five weeks and during a quiet period I installed QPython on my tablet with the aim of porting a programme that I'd written in C++ 15 years ago to Python. Cutting and pasting and even moving around the IDE turned out to be a truly frustrating exercise. I wonder if it was just my clumsiness or if others have had the same experience? -- Regards, Phil ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] List of ints
On Mon, Mar 2, 2015 at 10:50 PM, Phil wrote: > Thank you for reading this. > Python 3 under Linux. > > I'd like to set up a two dimensional list of counters as follows; > > count = [ > [0], > [0], > [0] > ] > Can you explain why the list is two-dimensional? It's not quite clear why. Do you have a particular use case in mind? > Is there a way to add a value to a list of ints? Can you give an example of what you'd like to see? Unfortunately, the word "add" is too ambiguous to know what the expectations are. If you can disambiguate with concrete examples, that may help us. Good luck! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] List of ints
On 03/03/15 06:50, Phil wrote: I'd like to set up a two dimensional list of counters as follows; count = [ [0], [0], [0] ] And then increment the first counter as follows; count [0] += 1 Are you trying to increment the zero to make it 1? Or are you trying to add a new value, 1, to the first sublist? ie Do you want the output to be: count = [ [1], [0], [0] ] OR count = [ [0,1], [0], [0] ] To do the first you need to increment the *value* of the first sublist not the sublist: count[0][0] += 1 To do the second you must use append(): count[0].append(1) The array module looks like the answer because it seems to function in the same way as an array under C. The array module is pretty specialized, in most cases a simple list is a better solution. Is there a way to add a value to a list of ints? Yes, see above. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] List of ints
On 03/03/2015 06:50, Phil wrote: Thank you for reading this. Python 3 under Linux. I'd like to set up a two dimensional list of counters as follows; count = [ [0], [0], [0] ] And then increment the first counter as follows; count [0] += 1 This fails with the following error; TypeError: 'int' object is not iterable The array module looks like the answer because it seems to function in the same way as an array under C. However, it seems to me that I should be able to do the same thing with a list. Is there a way to add a value to a list of ints? You are trying to increment the first element of count which is itself a list containing one element. You actually need:- count[0][0] +=1 -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] List of ints
Thank you for reading this. Python 3 under Linux. I'd like to set up a two dimensional list of counters as follows; count = [ [0], [0], [0] ] And then increment the first counter as follows; count [0] += 1 This fails with the following error; TypeError: 'int' object is not iterable The array module looks like the answer because it seems to function in the same way as an array under C. However, it seems to me that I should be able to do the same thing with a list. Is there a way to add a value to a list of ints? -- Regards, Phil ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor