Page 1 of 9 12345 ... LastLast
Results 1 to 15 of 131

Thread: Any Python Programmers Out There??

  1. #1 | Top
    Join Date
    Sep 2020
    Posts
    222
    Thanks
    2
    Thanked 68 Times in 58 Posts
    Groans
    0
    Groaned 2 Times in 2 Posts

    Default Any Python Programmers Out There??

    Seriously. Are any professional coders (Or even experienced amateurs) who use Python reading this forum??

  2. #2 | Top
    Join Date
    Feb 2020
    Posts
    87,041
    Thanks
    35,070
    Thanked 21,784 Times in 17,103 Posts
    Groans
    985
    Groaned 2,343 Times in 2,262 Posts
    Blog Entries
    1

    Default

    Quote Originally Posted by Bubba View Post
    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.

  3. #3 | Top
    Join Date
    Sep 2020
    Posts
    222
    Thanks
    2
    Thanked 68 Times in 58 Posts
    Groans
    0
    Groaned 2 Times in 2 Posts

    Default

    Quote Originally Posted by AProudLefty View Post
    Never used Python. Used C/C++/Java/Javascript/ASM/HTML5 and such though.
    OK, thanks for replying.

  4. #4 | Top
    Join Date
    Nov 2013
    Location
    internet
    Posts
    759
    Thanks
    127
    Thanked 366 Times in 247 Posts
    Groans
    5
    Groaned 24 Times in 23 Posts

    Default

    Quote Originally Posted by Bubba View Post
    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
    NPC is a beloved internet personality. Their pronouns are he/him. NPC fully recognizes the inherent privilege of being able to post on a message board, and as such NPC makes sure to avoid all social media where the trust and safety council flags something as dangerous to think about. He avoids all tweets that are disputed, and only supports mainstream news sources where former CIA, NSA and FBI serve as talking heads.The future is female, orange man bad, black lives matter, and wear a damn mask!

  5. #5 | Top
    Join Date
    Sep 2020
    Posts
    222
    Thanks
    2
    Thanked 68 Times in 58 Posts
    Groans
    0
    Groaned 2 Times in 2 Posts

    Default

    Quote Originally Posted by Niche Political Commentor View Post
    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 [I] == 0:
    ------- PLst [I] = 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 [I] == 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.

  6. #6 | Top
    Join Date
    Feb 2020
    Posts
    87,041
    Thanks
    35,070
    Thanked 21,784 Times in 17,103 Posts
    Groans
    985
    Groaned 2,343 Times in 2,262 Posts
    Blog Entries
    1

    Default

    Try randint(1,9) + 1

  7. #7 | Top
    Join Date
    Feb 2020
    Posts
    87,041
    Thanks
    35,070
    Thanked 21,784 Times in 17,103 Posts
    Groans
    985
    Groaned 2,343 Times in 2,262 Posts
    Blog Entries
    1

    Default

    Quote Originally Posted by Bubba View Post
    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 [I] == 0:
    ------- PLst [I] = 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 [I] == 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.

  8. #8 | Top
    Join Date
    Nov 2013
    Location
    internet
    Posts
    759
    Thanks
    127
    Thanked 366 Times in 247 Posts
    Groans
    5
    Groaned 24 Times in 23 Posts

    Default

    Quote Originally Posted by Bubba View Post

    Traceback (most recent call last):
    File "C:/Users/Sam/AppData/Local/Programs/Python/Python38/Scripts/Fourth Prac.py", line 27, in <module>
    if PLst [I] == 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 [I] == 0:
    ------- PLst [I] = 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[i] == 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[I] = 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 by Niche Political Commentor; 12-28-2020 at 01:30 AM.
    NPC is a beloved internet personality. Their pronouns are he/him. NPC fully recognizes the inherent privilege of being able to post on a message board, and as such NPC makes sure to avoid all social media where the trust and safety council flags something as dangerous to think about. He avoids all tweets that are disputed, and only supports mainstream news sources where former CIA, NSA and FBI serve as talking heads.The future is female, orange man bad, black lives matter, and wear a damn mask!

  9. The Following User Says Thank You to Niche Political Commentor For This Post:

    AProudLefty (12-28-2020)

  10. #9 | Top
    Join Date
    Feb 2020
    Posts
    87,041
    Thanks
    35,070
    Thanked 21,784 Times in 17,103 Posts
    Groans
    985
    Groaned 2,343 Times in 2,262 Posts
    Blog Entries
    1

    Default

    Quote Originally Posted by Niche Political Commentor View Post
    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[i] == 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.

  11. #10 | Top
    Join Date
    Nov 2013
    Location
    internet
    Posts
    759
    Thanks
    127
    Thanked 366 Times in 247 Posts
    Groans
    5
    Groaned 24 Times in 23 Posts

    Default

    Quote Originally Posted by AProudLefty View Post
    Forget my other post. I figured it out.

    for I in range(7): will make it work.
    this would be like having a door that always falls off the hinges when you open it and your solution being to not open the door.
    NPC is a beloved internet personality. Their pronouns are he/him. NPC fully recognizes the inherent privilege of being able to post on a message board, and as such NPC makes sure to avoid all social media where the trust and safety council flags something as dangerous to think about. He avoids all tweets that are disputed, and only supports mainstream news sources where former CIA, NSA and FBI serve as talking heads.The future is female, orange man bad, black lives matter, and wear a damn mask!

  12. The Following User Says Thank You to Niche Political Commentor For This Post:

    ThatOwlWoman (12-28-2020)

  13. #11 | Top
    Join Date
    Nov 2013
    Location
    internet
    Posts
    759
    Thanks
    127
    Thanked 366 Times in 247 Posts
    Groans
    5
    Groaned 24 Times in 23 Posts

    Default

    Quote Originally Posted by AProudLefty View Post
    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)
    NPC is a beloved internet personality. Their pronouns are he/him. NPC fully recognizes the inherent privilege of being able to post on a message board, and as such NPC makes sure to avoid all social media where the trust and safety council flags something as dangerous to think about. He avoids all tweets that are disputed, and only supports mainstream news sources where former CIA, NSA and FBI serve as talking heads.The future is female, orange man bad, black lives matter, and wear a damn mask!

  14. #12 | Top
    Join Date
    Feb 2020
    Posts
    87,041
    Thanks
    35,070
    Thanked 21,784 Times in 17,103 Posts
    Groans
    985
    Groaned 2,343 Times in 2,262 Posts
    Blog Entries
    1

    Default

    Quote Originally Posted by Niche Political Commentor View Post
    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?

  15. #13 | Top
    Join Date
    Apr 2009
    Posts
    108,120
    Thanks
    60,501
    Thanked 35,051 Times in 26,519 Posts
    Groans
    47,393
    Groaned 4,742 Times in 4,521 Posts
    Blog Entries
    61

    Default

    My son Josh is a professional programmer working for Softbank in Tokyo. He is most proficient in Python, C++ and Java.

  16. #14 | Top
    Join Date
    Sep 2020
    Posts
    222
    Thanks
    2
    Thanked 68 Times in 58 Posts
    Groans
    0
    Groaned 2 Times in 2 Posts

    Default

    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.

  17. The Following 2 Users Say Thank You to Bubba For This Post:

    AProudLefty (12-28-2020), ThatOwlWoman (12-28-2020)

  18. #15 | Top
    Join Date
    Nov 2013
    Location
    internet
    Posts
    759
    Thanks
    127
    Thanked 366 Times in 247 Posts
    Groans
    5
    Groaned 24 Times in 23 Posts

    Default

    Quote Originally Posted by AProudLefty View Post
    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[i] == 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 by Niche Political Commentor; 12-28-2020 at 02:08 AM.
    NPC is a beloved internet personality. Their pronouns are he/him. NPC fully recognizes the inherent privilege of being able to post on a message board, and as such NPC makes sure to avoid all social media where the trust and safety council flags something as dangerous to think about. He avoids all tweets that are disputed, and only supports mainstream news sources where former CIA, NSA and FBI serve as talking heads.The future is female, orange man bad, black lives matter, and wear a damn mask!

  19. The Following User Says Thank You to Niche Political Commentor For This Post:

    AProudLefty (12-28-2020)

Similar Threads

  1. A Monty Python riddle for you......
    By Cancel 2020.2 in forum Off Topic Forum
    Replies: 0
    Last Post: 04-27-2020, 07:44 PM
  2. Replies: 2
    Last Post: 06-16-2018, 08:07 PM
  3. Monty Python meets Daesh
    By cancel2 2022 in forum Off Topic Forum
    Replies: 69
    Last Post: 01-13-2016, 06:00 PM
  4. 3 S.F. programmers build alternative to HealthCare.gov
    By StormX in forum Current Events Forum
    Replies: 17
    Last Post: 11-11-2013, 04:55 PM
  5. Pet Python Strangles Child, Officials Say
    By Demwit in forum Off Topic Forum
    Replies: 1
    Last Post: 07-01-2009, 07:39 PM

Bookmarks

Posting Rules

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •