Any Python Programmers Out There??

My apologies. (A bit drunk heh)

Okay the list is [2, 0, 4, 5, 7, 0, 3, 2]

The indexes are 0 to 6, correct?

See the number 7 there? It goes outside the index. Thus the error.

What I am unable to figure out is why it doesn't detect the error all the time.

My bad, the indexes are 0 to 7.
 
My apologies. (A bit drunk heh)

Okay the list is [2, 0, 4, 5, 7, 0, 3, 2]

The indexes are 0 to 6, correct?

See the number 7 there? It goes outside the index. Thus the error.

What I am unable to figure out is why it doesn't detect the error all the time.

the indexes are 0-7

and yes we have established his error is due to citing indexes out of range of the list.

and I literally explained in great detail why it doesn't throw an error every time. It has to do SPECIFICALLY with the 4th iteration in his for loop.

The way he's written the code, he's never evaluating the element he wants to. This is because he is using

if list[item] == 0: <--- don't do this

rather than just simply using

if item == 0:

The former uses the current element as an index, and then evaluates the element it points to. The latter evaluates the current element itself.

On the first iteration he's evaluating the third element of the list. On the second iteration, the 1st element, on the third iteration, the 5th, etc.

Once he is on the 4th iteration he ends up actually evaluating for the 6th element (0). Then when evaluating for this element, he then CHANGES this element with the randint function. Then the for loop continues to progress. If by the time the for loop gets to the now updated 6th element and it's randed an 8 or 9, the script crashes because of the index being evaluated is out of range. If it is not randed an 8 or 9, then the script will continue to run. But it might change and unchange the list numerous times completely haphazardly.

TL;DR - use the code I posted above for great success
 
Last edited:
the indexes are 0-7

and yes we have established his error is due to citing indexes out of range of the list.

and I literally explained in great detail why it doesn't throw an error every time. It has to do SPECIFICALLY with the 4th iteration in his for loop.

The way he's written the code, he's never evaluating the element he wants to. On the first iteration he's evaluating the third element of the list. On the second iteration, the 1st element, on the third iteration, the 5th, etc.

Once he is on the 4th iteration he ends up actually evaluating for the 6th element (0). Then when evaluating for this element, he then CHANGES this element with the randint function. Then the for loop continues to progress. If by the time the for loop gets to the now updated 6th element and it's randed an 8 or 9, the script crashes because of the index being evaluated is out of range.

TL;DR - use the code I posted above for great success

Try this

from random import randint
PLst = [2, 0, 4, 5, 7, 0, 3, 2]
for I in PLst:
print (I)

It's always the same results.
 
Sorry I modified the code. And as I have said I am drunk. Sorry. Drunk programming. ;)

Try this one.

from random import randint
PLst = [2, 0, 4, 5, 7, 0, 3, 2]
Shithead = [0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0, 0]
for I in PLst:
if PLst == 0:
Shithead = randint(1,9)
print (Shithead)
 
Sorry I modified the code. And as I have said I am drunk. Sorry. Drunk programming. ;)

Try this one.

from random import randint
PLst = [2, 0, 4, 5, 7, 0, 3, 2]
Shithead = [0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0, 0]
for I in PLst:
if PLst == 0:
Shithead = randint(1,9)
print (Shithead)


ok lol I dont know why you keep ignoring me on this.

DO. NOT. USE. "if PLst == 0" Don't. STOP!

The conditional should be "if i == 0"
 
LOL. I am not ignoring. I am trying to figure out why it keep displaying the error message while a few times it doesn't.

I wonder if it's a bug.

Everything has already been explained by me like 3 times in this thread by now. You'll just have to sober up until you get it. I wrote the code out above that works and does exactly what OP wants.

Main takeaway, in a for loop if you wish to evaluate an element, you do so by doing if element = x. Not if list[element] = x. These two things are very different and will give very different results.
 
Last edited:
ok lol I dont know why you keep ignoring me on this.

DO. NOT. USE. "if PLst == 0" Don't. STOP!

The conditional should be "if i == 0"


HOLY FUCK! I just figured it out! No bug.

Try this.

from random import randint
PLst = [2, 0, 4, 5, 7, 0, 3, 2]
Shithead = [0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0, 0]
for I in PLst:
Shithead = randint(1,9)
print (Shithead)
 
Everything has already been explained by me like 3 times in this thread by now. You'll just have to sober up until you get it. I wrote the code out above that works and does exactly what OP wants.

Main takeaway, in a for loop you evaluate an element by doing if element = x. Not if list[element] = x. These two things are very different and will give very different results.

I know. Both of you and I have explained. That isn't what I am talking about. What I am talking about is why the error keep appearing.
 
no, your explanation was wrong. it didn't address the core issue.

As per the OP:

"So it can't continue on and go out of range. Can it??"

"Traceback (most recent call last):
File "C:/Users/Sam/AppData/Local/Programs/Python/Python38/Scripts/Fourth Prac.py", line 27, in <module>
if PLst == 0:
IndexError: list index out of range

Totally do not understand"

He wants to understand what the error was.

Even after his program works (after thinking and suggestions from us), he wants to understand for future reference.

"how can I avoid the error message."

Debugging, assert and testing do the trick.
 
As per the OP:

"So it can't continue on and go out of range. Can it??"

"Traceback (most recent call last):
File "C:/Users/Sam/AppData/Local/Programs/Python/Python38/Scripts/Fourth Prac.py", line 27, in <module>
if PLst == 0:
IndexError: list index out of range

Totally do not understand"

He wants to understand what the error was.

Even after his program works (after thinking and suggestions from us), he wants to understand for future reference.

"how can I avoid the error message."

Debugging, assert and testing do the trick.


yes and I explained it as basically as I can. his issue is wrongly trying to evaluate an element by doing if list[item] == 0
 
yes and I explained it as basically as I can. his issue is wrongly trying to evaluate an element by doing if list[item] == 0

Yeah I apologize. You and I recognize the error. Basically the 7th number is modified with randint(1,9) before it is evaluated.

I am hoping that the OP will understand it completely. It looks like you are an experienced/professional programmer yourself.
 
[2, _, 4, 5, 7, _, 3, 2] Underscores are actually blank boxes. Idea is to fill in the blanks with numbers 1 - 9 that make the line add up to 30. Seems to me this code SHOULD work:

from random import randint
PLst = [2, 0, 4, 5, 7, 0, 3, 2]
for I in PLst:
--- if PLst == 0:
------- PLst = randint (1, 9)
print (PLst, sum (PLst))

.


0 1 2 3 4 5 6 7 index
2 0 4 5 7 0 3 2 the list
4 2 7 0 2 2 5 4 indices in PLst
Do you notice the number 5?
PLst[5] == 0. So it is modified with randint(1,9) before it is evaluated.
 
Back
Top