[Python] TypeError: 'int' object is not subscriptable in samply python code [closed]

Discussão em 'Python' iniciado por Stack, Fevereiro 27, 2025.

  1. Stack

    Stack Membro Participativo

    I am playing with this project https://github.com/thabbott/prisoners-dilemma

    and when running tournament.py I get that error in this block of code.

    TypeError: 'int' object is not subscriptable in sample python code

    from itertools import combinations
    from random import shuffle
    import time
    import numpy as np
    import matplotlib.pyplot as plt
    import ctypes

    """
    Prisoners' dilemma tournament
    """
    class Tournament():

    """
    Initialize the tournament

    Parameters
    ----------
    prisoners: list of competing Prisoner subclasses
    n_rounds: rounds per match
    n_repl: number of replicants of each species
    """
    def __init__(self, species, n_rounds, n_repl):
    self.species = species
    self.frequency = len(self.species) * [0.0]
    for ii in range(len(self.species)):
    self.frequency[ii] = 1.0 * n_repl[ii] / sum(n_repl)
    self.population_size = sum(n_repl)
    self.n_rounds = n_rounds
    self.fitness_history = []
    self.frequency_history = []
    self.fitness_history.append(len(self.species) * [np.nan])
    self.frequency_history.append(self.frequency.copy())
    self.is_extinct = len(self.species) * [False]
    self.repopulate()


    specifically at this line

    self.frequency[ii] = 1.0 * n_repl[ii] / sum(n_repl)


    I am trying to figure out what it is trying to do. I get that it is indexing an integer but I don't understand the original intent or how to fix it.

    anyone have any ideas?

    Continue reading...

Compartilhe esta Página