Skip to content
Advertisement

Python array as a counter

I’m a Python 3.0 beginner and I’m struggling to find a solution to the following problem. It’s a step for a Galton Board simulation exercise.

  1. I have an numpy array, which length is auto generated based on another variable
  2. I am then generating a set of numbers, adding them to a list and summing them
  3. I would finally want to add a counter of +1 to the array index, which equals the number in the sum in step 2.
JavaScript

I get an IndexError: only integers, slices (:), ellipsis (), numpy.newaxis (None) and integer or boolean arrays are valid indices error and don`t understand why that is.

Advertisement

Answer

JavaScript

In this line, you are using str(sum(…)), a string as index. Although string is iterable, it cannot be used as an index.

In your case, I would do behaelter[int(j)] to change ‘j’ into an integer so that you can use it as an index.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement