Any Python Programmers Out There??

Bubba

New member
Seriously. Are any professional coders (Or even experienced amateurs) who use Python reading this forum??
 
I'm not pro but yes I know and use python quite often

what's your question

Thanks for responding.

Briefly: I was a pretty good amateur programmer back in the days of DOS QuickBasic. My programs were structured, concise, and top-down - Another QuickBasic programmer woulda been able to read and modify my code easily. Object-Oriented Programming came along and pretty much put an end to QuickBasic. I took up Delphi/Pascal and became somewhat proficient. Started to learn C, but gave it up so I could concentrate on Excel VBA.

A few days ago, less than a week, I decided to learn Python. It is VERY different from anything I've seen before. Scoured the Internet and found a lot of good resources. Some free, some charge a fee.

Already solved and forgotten my original question when I posted the OP. But new problems keep cropping up. Got this from a book of puzzles.

[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))

At this point, I'm not trying to get the line to add to 30. Just trying to get the code to work. I get one of three results when I run it.

[2, 2, 4, 5, 7, 3, 3, 2]

See?? That time it worked! Does that once in a while, but not often. Usually I get:

[2, 0, 4, 5, 7, 3, 3, 2]

This time it changed one of the two zeros to a random number but ignored the other. The other result I get sometimes:

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. The `for' loop is supposed to stop when it runs out of subscripts. Well, isn't it?? So it can't continue on and go out of range. Can it??

So I wanna know how I can get the program to consistently replace the zeros with a random number and how can I avoid the error message.

Appreciate any light you could shed, Niche Political Commentor. Cannot find this situation addressed on any web sites I've studied.
 
Thanks for responding.

Briefly: I was a pretty good amateur programmer back in the days of DOS QuickBasic. My programs were structured, concise, and top-down - Another QuickBasic programmer woulda been able to read and modify my code easily. Object-Oriented Programming came along and pretty much put an end to QuickBasic. I took up Delphi/Pascal and became somewhat proficient. Started to learn C, but gave it up so I could concentrate on Excel VBA.

A few days ago, less than a week, I decided to learn Python. It is VERY different from anything I've seen before. Scoured the Internet and found a lot of good resources. Some free, some charge a fee.

Already solved and forgotten my original question when I posted the OP. But new problems keep cropping up. Got this from a book of puzzles.

[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))

At this point, I'm not trying to get the line to add to 30. Just trying to get the code to work. I get one of three results when I run it.

[2, 2, 4, 5, 7, 3, 3, 2]

See?? That time it worked! Does that once in a while, but not often. Usually I get:

[2, 0, 4, 5, 7, 3, 3, 2]

This time it changed one of the two zeros to a random number but ignored the other. The other result I get sometimes:

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. The `for' loop is supposed to stop when it runs out of subscripts. Well, isn't it?? So it can't continue on and go out of range. Can it??

So I wanna know how I can get the program to consistently replace the zeros with a random number and how can I avoid the error message.

Appreciate any light you could shed, Niche Political Commentor. Cannot find this situation addressed on any web sites I've studied.


Forget my other post. I figured it out.

for I in range(7): will make it work.
 
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. The `for' loop is supposed to stop when it runs out of subscripts. Well, isn't it?? So it can't continue on and go out of range. Can it??

So I wanna know how I can get the program to consistently replace the zeros with a random number and how can I avoid the error message.

Appreciate any light you could shed, Niche Political Commentor. Cannot find this situation addressed on any web sites I've studied.


yeah ok, so you are writing this really weird.

for I in PLst:
--- if PLst == 0:
------- PLst = randint (1, 9)
print (PLst, sum (PLst))


I'll explain what is happening, but the cliffs is that (Bold) is your issue. You are already iterating over a list with a for loop, but when using your first conditional, you are referencing the list by the index, using your current list element, and it is screwing everything up.

So lets back up for a moment.

Your list is this: [2,0,4,5,7,0,3,2]

The for loop is going to look at each element of the list.

The first element in a list is always referenced by list[0]. So in this case list[0] is 2.

With the way you wrote it, on your first iteration python looks at which element i is. In this case it is 2. So then when you write your conditional if plist == 0 what you are really asking for is plist[2], which is actually referencing the THIRD element of the list (4). So you are literally hoping all over the place for no reason.

Then on the next iteration when you get to the second element of your list, [2,0,4,5,7,9,3,2], you end up looking at plist[0] which points to (2), the first item in your list.

Third iteration: plist[4] (references 7 [2,0,4,5,7,0,3,2])
Fourth iteration: plist[5] (references 0! [2,0,4,5,7,0,3,2]) (so now that we have an element == 0, we are now updating plist[5] to something else. Lets say it rands 9.

PLst = randint (1, 9) ## plist[5] (which is 0) is now == 9


Fifth iteration: plist[7] (referencing 2, the last 2 in the list [2,0,4,5,7,9,3,2])
Sixth iteration (now remember, we have updated this number and it randed 9!) [2,0,4,5,7,9,3,2]

Except, now when we try to look up the list for plist[9] -

OOPS, plist[9] is out of range because there are not 10 items in this list, so your program crashes. The list can only be referenced up to list[7] because there are 8 elements in the list total. That is what your "index out or range" traceback is saying. That's why it is happening.

The above also explains why sometimes elements are updating and other times not updating. The rand on the 4th iteration will end up pointing the index to different locations when it gets to the 6th iteration in the for loop. The reason it doesn't happen often is because you have to rand a 1. So it's only gonna happen like 11% of the time.

So yeah, write your code the following way:

for index, i in enumerate(plist):
----if i == 0:
--------plist[index] = randint(1,9)

and you'll be all set. What this does is allows you to update the actual index that corresponds to where you are in the for loop.
 
Last edited:
yeah ok, so you are writing this really weird.



I'll explain what is happening, but the cliffs is that (Bold) is your issue. You are already iterating over a list with a for loop, but when using your first conditional, you are referencing the list by the index, and it is screwing everything up.

So lets back up for a moment.

Your list is this: [2,0,4,5,7,0,3,2]

The for loop is going to look at each element of the list.

The first element in a list is always referenced by list[0]. So in this case list[0] is 2.

With the way you wrote it, on your first iteration python looks at plist[0] which is the first element of the list (2), and then when you write your conditional if list == 0 what you are really asking for is list[2], which is actually referencing the THIRD element of the list (4). So you are literally hoping all over the place for no reason.

Then on the next iteration when you get to the second element of your list, [2,0,4,5,7,9,3,2], you end up looking at plist[0] which points to (2), the first item in your list.

Third iteration: list[4] (referencing 7 [2,0,4,5,7,0,3,2])
Fourth iteration: list[5] (referencing 0! [2,0,4,5,7,0,3,2]) (so now that we have an element = 0, we are now updating list[5] to something else. Lets say it rands 9.
Fifth iteration: list[7] (referencing 2, the last 2 in the list [2,0,4,5,7,9,3,2])
Sixth iteration (now remember, we have updated this number and it randed 9!)

Except, now when we try to look up the list for plist[9] -

OOPS, plist[9] is out of range because there are not 10 items in this list, so your program crashes. The list can only be referenced up to list[7] because there are 8 elements in the list total.

So yeah, write your code the following way:

for index, i in enumerate(plist):
----if i == 0:
--------plist[index] = randint(1,9)

and you'll be all set.


Yeah that would explain it. As I have posted, the range method works. Though I do not know how to dynamically count the number of items in a list.
 
Thanks APL and NPC. Can't say I understand. I'll read over your comments tomorrow and keep plugging away. You say I'm writing it weird, but that's the only way I know how to write it.
 
Yes heh. I figured it out. What I cannot figure out yet is why it doesn't detect the error all the time?

read my post above again and look at how the code logic will work. The reason it doesn't always crash is because as long as it rands lower than 8, there wont be any issues, as it will never reference out of index within the list.

OPs problem is doing the "if list == 0" conditional, because that is simply not necessary and not done in for loops and is leading to him bouncing all over the place in the list. In fact, depending on how he rands, he's typically missing evaluating the 2nd and 7th elements in his list every single time. So his way doesn't even allow him to evaluate the elements properly.
 
Last edited:
Thanks APL and NPC. Can't say I understand. I'll read over your comments tomorrow and keep plugging away. You say I'm writing it weird, but that's the only way I know how to write it.

I'm literally telling you what you are doing wrong. When evaluating elements of a list in a for loop, don't do: list. Because you wont be evaluating the element you want.

Just do:

for item in list:
----if item == 0:
--------<other stuff goes here>

for your specific task, you are gonna wanna do this:

for index, item in enumerate(list):
----if item == 0:
--------list[index] = randint(1,9)
print(list, sum(list))

do this and it will work.
 
read my post above again and look at how the code logic will work. The reason it doesn't always crash because as long as it rands lower than 8, there wont be any issues, as it will never reference out of index within the list.

OPs problem is doing the "if list == 0" conditional, because that is simply not necessary and not done in for loops and is leading to him bouncing all over the place in the list. In fact, depending on how he rands, he's typically missing evaluating the 2ndd and 7th elements in his list every single time. So his way doesn't even allow him to evaluate the elements properly.


Yeah 7th is what generates the error. Haven't done Python but I am an experienced programmer.

for I in PLst pulls out each number in the list. 7th goes outside the list.

The problem here is that, if you would print the I's, it will end up with the same sequence of the numbers in the list. So how come it goes outside the list most of the time and not the other times?
 
Yeah 7th is what generates the error. Haven't done Python but I am an experienced programmer.

for I in PLst pulls out each number in the list. 7th goes outside the list.

The problem here is that, if you would print the I's, it will end up with the same sequence of the numbers in the list. So how come it goes outside the list most of the time and not the other times?

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.
 
Here is me running the script a billion times and having zero issues:

sCPv4fb.png


code:

DNUg1cf.png
 
Back
Top