Python Forum
Message='int' object is not subscriptable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Message='int' object is not subscriptable
#5
Your method for randomly choosing coordinates is very inefficient. Here's a little program that counts how long it takes to randomly select 64 different numbers in the range 1..64.
import random

for _ in range(10):
    used_spots = set()
    i = 0
    while len(used_spots) < 64:
        used_spots.add(random.randint(1, 64))
        i += 1
    print(i)
Output:
309 478 209 320 306 452 295 221 393 327
You do not put cards back in the deck while dealing a hand, so why not use the same kind of subtractive logic for coordinates. This code makes a "deck" of coordinates and shuffles the deck. To get a random coordinate you just deal the top card.
import random

random_coords = [r+c for r in 'abcdefgh' for c in '12345678']  # Make list of all coordinates
random.shuffle(random_coords)

for _ in range(5):
    print(random_coords.pop())  # Deal the top card
Reply


Messages In This Thread
RE: Message='int' object is not subscriptable - by deanhystad - Aug-10-2021, 05:21 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 766 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  Help with python 'not subscriptable' error Extra 3 2,142 Dec-16-2022, 05:55 PM
Last Post: woooee
  TypeError: 'NoneType' object is not subscriptable syafiq14 3 5,295 Sep-19-2022, 02:43 PM
Last Post: Larz60+
  'int' object is not subscriptable after API call ed8484 1 1,835 Sep-18-2021, 02:06 PM
Last Post: ed8484
  Bool Object is not Subscriptable quest 1 4,188 May-02-2021, 11:12 AM
Last Post: Yoriz
  Float Object is not Subscriptable quest 2 2,917 Apr-20-2021, 09:28 AM
Last Post: quest
  AttributeError: 'Message' object has no attribute 'split' helpme1 2 5,035 Mar-14-2021, 11:25 AM
Last Post: helpme1
  TypeError: 'NoneType' object is not subscriptable Jmekubo 6 27,370 Sep-08-2020, 10:03 AM
Last Post: DigiTMG
  TypeError: 'type' object is not subscriptable Stef 1 4,560 Aug-28-2020, 03:01 PM
Last Post: Gribouillis
  'NoneType' object is not subscriptable Justchse 4 3,728 Aug-01-2020, 06:18 PM
Last Post: Justchse

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020