Seriously. Are any professional coders (Or even experienced amateurs) who use Python reading this forum??
Never used Python. Used C/C++/Java/Javascript/ASM/HTML5 and such though.
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.
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.
for I in PLst:
--- if PLst == 0:
------- PLst = randint (1, 9)
print (PLst, sum (PLst))
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.
Forget my other post. I figured it out.
for I in range(7): will make it work.
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.
as I said above "just dont rand higher than 7" isn't a very effective solution, lol.
number of items in a list is len(list)
Yes heh. I figured it out. What I cannot figure out yet is why it doesn't detect the error all the time?
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.
My son Josh is a professional programmer working for Softbank in Tokyo. He is most proficient in Python, C++ and Java.
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?