Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #77485 > unrolled thread
| Started by | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| First post | 2014-09-03 14:10 -0400 |
| Last post | 2014-09-03 21:15 -0700 |
| Articles | 20 on this page of 49 — 17 participants |
Back to article view | Back to comp.lang.python
Python is going to be hard Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-03 14:10 -0400
Re: Python is going to be hard John Gordon <gordon@panix.com> - 2014-09-03 18:17 +0000
Re: Python is going to be hard Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-03 14:52 -0400
Re: Python is going to be hard mm0fmf <none@mailinator.com> - 2014-09-03 22:37 +0100
Re: Python is going to be hard Rock Neurotiko <miguelglafuente@gmail.com> - 2014-09-03 20:16 +0200
Re: Python is going to be hard Rob Gaddi <rgaddi@technologyhighland.invalid> - 2014-09-03 11:19 -0700
Re: Python is going to be hard Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-03 14:50 -0400
Re: Python is going to be hard MRAB <python@mrabarnett.plus.com> - 2014-09-03 19:24 +0100
Re: Python is going to be hard Skip Montanaro <skip@pobox.com> - 2014-09-03 13:28 -0500
Re: Python is going to be hard Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-03 14:52 -0400
Re: Python is going to be hard Ethan Furman <ethan@stoneleaf.us> - 2014-09-03 11:33 -0700
Re: Python is going to be hard Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-03 14:41 -0400
Re: Python is going to be hard Ethan Furman <ethan@stoneleaf.us> - 2014-09-03 12:49 -0700
Re: Python is going to be hard Juan Christian <juan0christian@gmail.com> - 2014-09-03 15:44 -0300
Re: Python is going to be hard Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-03 14:56 -0400
Re: Python is going to be hard Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-03 14:49 -0400
Re: Python is going to be hard Ethan Furman <ethan@stoneleaf.us> - 2014-09-03 11:55 -0700
Re: Python is going to be hard Rob Gaddi <rgaddi@technologyhighland.invalid> - 2014-09-03 12:01 -0700
Re: Python is going to be hard Ian Kelly <ian.g.kelly@gmail.com> - 2014-09-03 13:11 -0600
Re: Python is going to be hard Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-03 15:22 -0400
Re: Python is going to be hard Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-04 12:11 +1000
Re: Python is going to be hard Denis McMahon <denismfmcmahon@gmail.com> - 2014-09-03 20:55 +0000
Re: Python is going to be hard Rustom Mody <rustompmody@gmail.com> - 2014-09-03 18:48 -0700
Re: Python is going to be hard Chris Angelico <rosuav@gmail.com> - 2014-09-04 11:56 +1000
Re: Python is going to be hard Rustom Mody <rustompmody@gmail.com> - 2014-09-03 19:10 -0700
Re: Python is going to be hard Chris Angelico <rosuav@gmail.com> - 2014-09-04 12:25 +1000
Re: Python is going to be hard Rustom Mody <rustompmody@gmail.com> - 2014-09-03 19:33 -0700
Re: Python is going to be hard alister <alister.nospam.ware@ntlworld.com> - 2014-09-04 10:29 +0000
Re: Python is going to be hard Rustom Mody <rustompmody@gmail.com> - 2014-09-04 06:08 -0700
Re: Python is going to be hard Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-04 23:25 +1000
Re: Python is going to be hard Chris Angelico <rosuav@gmail.com> - 2014-09-04 23:55 +1000
Re: Python is going to be hard Rustom Mody <rustompmody@gmail.com> - 2014-09-03 20:22 -0700
Re: Python is going to be hard Chris Angelico <rosuav@gmail.com> - 2014-09-04 13:49 +1000
Re: Python is going to be hard Rustom Mody <rustompmody@gmail.com> - 2014-09-03 21:11 -0700
Re: Python is going to be hard Chris Angelico <rosuav@gmail.com> - 2014-09-04 15:02 +1000
Re: Python is going to be hard Rustom Mody <rustompmody@gmail.com> - 2014-09-03 23:23 -0700
Re: Python is going to be hard Chris Angelico <rosuav@gmail.com> - 2014-09-04 16:39 +1000
Re: Python is going to be hard Rustom Mody <rustompmody@gmail.com> - 2014-09-04 06:15 -0700
Re: Python is going to be hard Chris Angelico <rosuav@gmail.com> - 2014-09-04 23:30 +1000
Re: Python is going to be hard Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-04 23:37 +1000
Re: Python is going to be hard Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-09-04 15:04 +0100
Re: Python is going to be hard Chris Angelico <rosuav@gmail.com> - 2014-09-05 00:08 +1000
Re: Python is going to be hard Rustom Mody <rustompmody@gmail.com> - 2014-09-04 19:24 -0700
Re: Python is going to be hard Chris Angelico <rosuav@gmail.com> - 2014-09-05 12:30 +1000
Re: Python is going to be hard Roy Smith <roy@panix.com> - 2014-09-04 22:51 -0400
Re: Python is going to be hard Rustom Mody <rustompmody@gmail.com> - 2014-09-04 19:56 -0700
Re: Python is going to be hard Chris Angelico <rosuav@gmail.com> - 2014-09-05 13:08 +1000
Re: Python is going to be hard Ethan Furman <ethan@stoneleaf.us> - 2014-09-03 21:06 -0700
Re: Python is going to be hard Rustom Mody <rustompmody@gmail.com> - 2014-09-03 21:15 -0700
Page 1 of 3 [1] 2 3 Next page →
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-09-03 14:10 -0400 |
| Subject | Python is going to be hard |
| Message-ID | <d9me0ap5s0s28qaeobbh6680gciel6c1og@4ax.com> |
import math
import random
import sys
b=[]
steve = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
for x in steve:
print (steve[x])
Traceback (most recent call last):
File "C:\Functions\blank.py", line 7, in <module>
print (steve[x])
IndexError: list index out of range
[toc] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2014-09-03 18:17 +0000 |
| Message-ID | <lu7lvn$jrc$1@reader1.panix.com> |
| In reply to | #77485 |
In <d9me0ap5s0s28qaeobbh6680gciel6c1og@4ax.com> Seymore4Head <Seymore4Head@Hotmail.invalid> writes: > import math > import random > import sys Why are you importing these modules if they're not used? > b=[] Likewise b is not used. > steve = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] > for x in steve: > print (steve[x]) As you step through the loop, x becomes each successive item in the 'steve' list. Therefore, you probably want to print just plain x, not steve[x]. > Traceback (most recent call last): > File "C:\Functions\blank.py", line 7, in <module> > print (steve[x]) > IndexError: list index out of range There are fewer than 13 items in steve, so when x reaches 13 this error pops up. -- John Gordon Imagine what it must be like for a real medical doctor to gordon@panix.com watch 'House', or a real serial killer to watch 'Dexter'.
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-09-03 14:52 -0400 |
| Message-ID | <rroe0a15lne0mk9s8drpputv1nhllorbec@4ax.com> |
| In reply to | #77486 |
On Wed, 3 Sep 2014 18:17:27 +0000 (UTC), John Gordon <gordon@panix.com> wrote: >In <d9me0ap5s0s28qaeobbh6680gciel6c1og@4ax.com> Seymore4Head <Seymore4Head@Hotmail.invalid> writes: > >> import math >> import random >> import sys > >Why are you importing these modules if they're not used? > >> b=[] > >Likewise b is not used. > >> steve = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] >> for x in steve: >> print (steve[x]) > >As you step through the loop, x becomes each successive item in the 'steve' >list. Therefore, you probably want to print just plain x, not steve[x]. > >> Traceback (most recent call last): >> File "C:\Functions\blank.py", line 7, in <module> >> print (steve[x]) >> IndexError: list index out of range > >There are fewer than 13 items in steve, so when x reaches 13 this error >pops up. I see that now. Thanks
[toc] | [prev] | [next] | [standalone]
| From | mm0fmf <none@mailinator.com> |
|---|---|
| Date | 2014-09-03 22:37 +0100 |
| Message-ID | <aTLNv.193655$9R5.32311@fx29.am4> |
| In reply to | #77497 |
On 03/09/2014 19:52, Seymore4Head wrote: > I see that now. > Thanks > Maybe some comments in your code would help you? And also posting an on-topic title would help too.
[toc] | [prev] | [next] | [standalone]
| From | Rock Neurotiko <miguelglafuente@gmail.com> |
|---|---|
| Date | 2014-09-03 20:16 +0200 |
| Message-ID | <mailman.13740.1409768250.18130.python-list@python.org> |
| In reply to | #77485 |
[Multipart message — attachments visible in raw view] — view raw
print(x) :) 2014-09-03 20:10 GMT+02:00 Seymore4Head <Seymore4Head@hotmail.invalid>: > import math > import random > import sys > b=[] > steve = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] > for x in steve: > print (steve[x]) > > Traceback (most recent call last): > File "C:\Functions\blank.py", line 7, in <module> > print (steve[x]) > IndexError: list index out of range > -- > https://mail.python.org/mailman/listinfo/python-list > -- Miguel García Lafuente - Rock Neurotiko Do it, the devil is in the details. The quieter you are, the more you are able to hear. Happy Coding. Code with Passion, Decode with Patience. If we make consistent effort, based on proper education, we can change the world. El contenido de este e-mail es privado, no se permite la revelacion del contenido de este e-mail a gente ajena a él.
[toc] | [prev] | [next] | [standalone]
| From | Rob Gaddi <rgaddi@technologyhighland.invalid> |
|---|---|
| Date | 2014-09-03 11:19 -0700 |
| Message-ID | <20140903111904.0500bd47@rg.highlandtechnology.com> |
| In reply to | #77485 |
On Wed, 03 Sep 2014 14:10:42 -0400 Seymore4Head <Seymore4Head@Hotmail.invalid> wrote: > import math > import random > import sys > b=[] > steve = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] > for x in steve: > print (steve[x]) > > Traceback (most recent call last): > File "C:\Functions\blank.py", line 7, in <module> > print (steve[x]) > IndexError: list index out of range You're failing to go through the basic tutorials, and blaming the language when you don't understand things. 'for x in steve' does not sweep x over the indices of steve. 'for x in steve' sweeps x over the sequential _values_ in steve. This would have been clear if you were to add a print(x) into the loop. -- Rob Gaddi, Highland Technology -- www.highlandtechnology.com Email address domain is currently out of order. See above to fix.
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-09-03 14:50 -0400 |
| Message-ID | <smoe0apq7ho415a8grhugn8gb27mq02e4k@4ax.com> |
| In reply to | #77488 |
On Wed, 3 Sep 2014 11:19:04 -0700, Rob Gaddi <rgaddi@technologyhighland.invalid> wrote: >On Wed, 03 Sep 2014 14:10:42 -0400 >Seymore4Head <Seymore4Head@Hotmail.invalid> wrote: > >> import math >> import random >> import sys >> b=[] >> steve = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] >> for x in steve: >> print (steve[x]) >> >> Traceback (most recent call last): >> File "C:\Functions\blank.py", line 7, in <module> >> print (steve[x]) >> IndexError: list index out of range > >You're failing to go through the basic tutorials, and blaming the >language when you don't understand things. > >'for x in steve' does not sweep x over the indices of steve. 'for x in >steve' sweeps x over the sequential _values_ in steve. This would have >been clear if you were to add a print(x) into the loop. Yes print(x) does make that clear Thanks
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2014-09-03 19:24 +0100 |
| Message-ID | <mailman.13741.1409768687.18130.python-list@python.org> |
| In reply to | #77485 |
On 2014-09-03 19:10, Seymore4Head wrote: > import math > import random > import sys > b=[] > steve = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] > for x in steve: > print (steve[x]) > > Traceback (most recent call last): > File "C:\Functions\blank.py", line 7, in <module> > print (steve[x]) > IndexError: list index out of range > Iterating over a list yields its contents, not indexes.
[toc] | [prev] | [next] | [standalone]
| From | Skip Montanaro <skip@pobox.com> |
|---|---|
| Date | 2014-09-03 13:28 -0500 |
| Message-ID | <mailman.13742.1409768928.18130.python-list@python.org> |
| In reply to | #77485 |
On Wed, Sep 3, 2014 at 1:24 PM, MRAB <python@mrabarnett.plus.com> wrote: > Iterating over a list yields its contents, not indexes. Unlike in JavaScript. Not sure where the OP is coming from, but that "feature" of JavaScript threw me when I first encountered it. My guess would be that his prior experience includes (at least) JS. Skip
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-09-03 14:52 -0400 |
| Message-ID | <vnoe0a1j3qr55bgmra831021s9te4b6ei2@4ax.com> |
| In reply to | #77490 |
On Wed, 3 Sep 2014 13:28:39 -0500, Skip Montanaro <skip@pobox.com> wrote: >On Wed, Sep 3, 2014 at 1:24 PM, MRAB <python@mrabarnett.plus.com> wrote: >> Iterating over a list yields its contents, not indexes. > >Unlike in JavaScript. Not sure where the OP is coming from, but that >"feature" of JavaScript threw me when I first encountered it. My guess >would be that his prior experience includes (at least) JS. > >Skip Actually it was BASIC some 30 years ago. I never got too good at BASIC and it doesn't look like I am going to get too good at Python either. :)
[toc] | [prev] | [next] | [standalone]
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Date | 2014-09-03 11:33 -0700 |
| Message-ID | <mailman.13743.1409769229.18130.python-list@python.org> |
| In reply to | #77485 |
On 09/03/2014 11:10 AM, Seymore4Head wrote: > import math > import random > import sys > b=[] > steve = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] > for x in steve: > print (steve[x]) > > Traceback (most recent call last): > File "C:\Functions\blank.py", line 7, in <module> > print (steve[x]) > IndexError: list index out of range Python will be incredibly hard if you don't read any of the docs or tutorials available. -- ~Ethan~
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-09-03 14:41 -0400 |
| Message-ID | <hune0a5bnnr3dpoj60jfob7smfmsk43esd@4ax.com> |
| In reply to | #77491 |
On Wed, 03 Sep 2014 11:33:46 -0700, Ethan Furman <ethan@stoneleaf.us> wrote: >On 09/03/2014 11:10 AM, Seymore4Head wrote: >> import math >> import random >> import sys >> b=[] >> steve = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] >> for x in steve: >> print (steve[x]) >> >> Traceback (most recent call last): >> File "C:\Functions\blank.py", line 7, in <module> >> print (steve[x]) >> IndexError: list index out of range > >Python will be incredibly hard if you don't read any of the docs or tutorials available. You can't accuse me of that. I have actually read quite a bit. I may not be picking it up, but I am trying.
[toc] | [prev] | [next] | [standalone]
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Date | 2014-09-03 12:49 -0700 |
| Message-ID | <mailman.13747.1409773766.18130.python-list@python.org> |
| In reply to | #77492 |
On 09/03/2014 11:41 AM, Seymore4Head wrote: > On Wed, 03 Sep 2014 11:33:46 -0700, Ethan Furman wrote: >> >> Python will be incredibly hard if you don't read any of the docs or tutorials available. > > You can't accuse me of that. I have actually read quite a bit. I may > not be picking it up, but I am trying. In that case I apologize for my remark. Keep trying -- once you get a handle on Python it is a very enjoyable language to use. -- ~Ethan~
[toc] | [prev] | [next] | [standalone]
| From | Juan Christian <juan0christian@gmail.com> |
|---|---|
| Date | 2014-09-03 15:44 -0300 |
| Message-ID | <mailman.13744.1409769914.18130.python-list@python.org> |
| In reply to | #77485 |
[Multipart message — attachments visible in raw view] — view raw
I'm learning Python using this mailist, and the Tutor mailist, reading the docs and watching this course, Python Fundamentals ( http://www.pluralsight.com/training/Courses/TableOfContents/python-fundamentals ). Python is really easy and useful, OP don't blame the language because you didn't understood it yet, just persist. On Wed, Sep 3, 2014 at 3:33 PM, Ethan Furman <ethan@stoneleaf.us> wrote: > On 09/03/2014 11:10 AM, Seymore4Head wrote: > >> import math >> import random >> import sys >> b=[] >> steve = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] >> for x in steve: >> print (steve[x]) >> >> Traceback (most recent call last): >> File "C:\Functions\blank.py", line 7, in <module> >> print (steve[x]) >> IndexError: list index out of range >> > > Python will be incredibly hard if you don't read any of the docs or > tutorials available. > > -- > ~Ethan~ > -- > https://mail.python.org/mailman/listinfo/python-list >
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-09-03 14:56 -0400 |
| Message-ID | <cuoe0a9rgsuos4s9n6q9p5s8p50fe8hq58@4ax.com> |
| In reply to | #77493 |
On Wed, 3 Sep 2014 15:44:47 -0300, Juan Christian <juan0christian@gmail.com> wrote: >I'm learning Python using this mailist, and the Tutor mailist, reading the >docs and watching this course, Python Fundamentals ( >http://www.pluralsight.com/training/Courses/TableOfContents/python-fundamentals >). > >Python is really easy and useful, OP don't blame the language because you >didn't understood it yet, just persist. > I don't think I have seen a link to that one yet. I have saved all the links I have seen posted here. I haven't tried all of them yet. Thanks
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-09-03 14:49 -0400 |
| Message-ID | <9eoe0apt76pbd5slks20bjaq24m5mrqe3f@4ax.com> |
| In reply to | #77485 |
On Wed, 03 Sep 2014 14:10:42 -0400, Seymore4Head <Seymore4Head@Hotmail.invalid> wrote: >import math >import random >import sys >b=[] >steve = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] >for x in steve: > print (steve[x]) > >Traceback (most recent call last): > File "C:\Functions\blank.py", line 7, in <module> > print (steve[x]) >IndexError: list index out of range Ok, I understand now that x is actually the first item in the list. What I want is a loop that goes from 1 to the total number of items in the list steve. Thanks
[toc] | [prev] | [next] | [standalone]
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Date | 2014-09-03 11:55 -0700 |
| Message-ID | <mailman.13745.1409770515.18130.python-list@python.org> |
| In reply to | #77494 |
On 09/03/2014 11:49 AM, Seymore4Head wrote: > On Wed, 03 Sep 2014 14:10:42 -0400, Seymore4Head > <Seymore4Head@Hotmail.invalid> wrote: > >> import math >> import random >> import sys >> b=[] >> steve = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] >> for x in steve: >> print (steve[x]) >> >> Traceback (most recent call last): >> File "C:\Functions\blank.py", line 7, in <module> >> print (steve[x]) >> IndexError: list index out of range > > Ok, I understand now that x is actually the first item in the list. > What I want is a loop that goes from 1 to the total number of items in > the list steve. No, you don't understand yet. The /first/ time through the loop 'x' is the first item in the list. The /second/ time through the loop 'x' is the second item in the list. The /third/ time through the loop 'x' is the third item in the list. . . . Keep persisting! -- ~Ethan~
[toc] | [prev] | [next] | [standalone]
| From | Rob Gaddi <rgaddi@technologyhighland.invalid> |
|---|---|
| Date | 2014-09-03 12:01 -0700 |
| Message-ID | <20140903120118.550e36ef@rg.highlandtechnology.com> |
| In reply to | #77498 |
On Wed, 03 Sep 2014 11:55:13 -0700 Ethan Furman <ethan@stoneleaf.us> wrote: > On 09/03/2014 11:49 AM, Seymore4Head wrote: > > On Wed, 03 Sep 2014 14:10:42 -0400, Seymore4Head > > <Seymore4Head@Hotmail.invalid> wrote: > > > >> import math > >> import random > >> import sys > >> b=[] > >> steve = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] > >> for x in steve: > >> print (steve[x]) > >> > >> Traceback (most recent call last): > >> File "C:\Functions\blank.py", line 7, in <module> > >> print (steve[x]) > >> IndexError: list index out of range > > > > Ok, I understand now that x is actually the first item in the list. > > What I want is a loop that goes from 1 to the total number of items in > > the list steve. > > No, you don't understand yet. > > The /first/ time through the loop 'x' is the first item in the list. > > The /second/ time through the loop 'x' is the second item in the list. > > The /third/ time through the loop 'x' is the third item in the list. > > . . . > > Keep persisting! > > -- > ~Ethan~ Python 'for' is better read as 'for each'. -- Rob Gaddi, Highland Technology -- www.highlandtechnology.com Email address domain is currently out of order. See above to fix.
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2014-09-03 13:11 -0600 |
| Message-ID | <mailman.13746.1409771560.18130.python-list@python.org> |
| In reply to | #77494 |
On Wed, Sep 3, 2014 at 12:49 PM, Seymore4Head
<Seymore4Head@hotmail.invalid> wrote:
> On Wed, 03 Sep 2014 14:10:42 -0400, Seymore4Head
> <Seymore4Head@Hotmail.invalid> wrote:
>
>>import math
>>import random
>>import sys
>>b=[]
>>steve = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
>>for x in steve:
>> print (steve[x])
>>
>>Traceback (most recent call last):
>> File "C:\Functions\blank.py", line 7, in <module>
>> print (steve[x])
>>IndexError: list index out of range
>
> Ok, I understand now that x is actually the first item in the list.
> What I want is a loop that goes from 1 to the total number of items in
> the list steve.
If you want the indexes also, you can do this:
for i, x in enumerate(steve):
print(i, x)
If you really want just the indexes and not the values, then you can do this:
for i in range(len(steve)):
print(i)
Most of the time though you will not need the indexes, and it will be
simpler just to work with the values by looping directly over the
list.
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-09-03 15:22 -0400 |
| Message-ID | <1dqe0a5p2eepuie3nhi6tiul7aah70jnjl@4ax.com> |
| In reply to | #77501 |
On Wed, 3 Sep 2014 13:11:51 -0600, Ian Kelly <ian.g.kelly@gmail.com> wrote: >On Wed, Sep 3, 2014 at 12:49 PM, Seymore4Head ><Seymore4Head@hotmail.invalid> wrote: >> On Wed, 03 Sep 2014 14:10:42 -0400, Seymore4Head >> <Seymore4Head@Hotmail.invalid> wrote: >> >>>import math >>>import random >>>import sys >>>b=[] >>>steve = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] >>>for x in steve: >>> print (steve[x]) >>> >>>Traceback (most recent call last): >>> File "C:\Functions\blank.py", line 7, in <module> >>> print (steve[x]) >>>IndexError: list index out of range >> >> Ok, I understand now that x is actually the first item in the list. >> What I want is a loop that goes from 1 to the total number of items in >> the list steve. > >If you want the indexes also, you can do this: > >for i, x in enumerate(steve): > print(i, x) > >If you really want just the indexes and not the values, then you can do this: > >for i in range(len(steve)): > print(i) > >Most of the time though you will not need the indexes, and it will be >simpler just to work with the values by looping directly over the >list. I figured it out now. I was expecting x to be a number and not an item. I used for i in range(len(steve)): Thanks Printing x to see what it is instead of assuming what it is really helps. I am getting there. I just have to take smaller steps.
[toc] | [prev] | [next] | [standalone]
Page 1 of 3 [1] 2 3 Next page →
Back to top | Article view | comp.lang.python
csiph-web