cancel2 2022
Canceled
thanks for the useless information
Well I was going to ask him to have a look, but fuck you muchly seems more in order.
thanks for the useless information
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 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. 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
Now I don't get any error. What the actual fuck?
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.
Now after 20 tries, I finally got this!
Traceback (most recent call last):
File "<string>", line 4, in <module>
IndexError: list index out of range
>
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.
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"
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.
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
[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))
.
Well I was going to ask him to have a look, but fuck you muchly seems more in order.