Home > OS >  Seemingly unrelated lines of Python code error when the order is changed
Seemingly unrelated lines of Python code error when the order is changed

Time:07-20

Could anyone tell me why I get an error based on the order of these seemingly unrelated line of code? If I call the numpy randint function before the loop I encounter no error but if I run the randint function after the loop I get this error:

  File "C:\Users\natha\OneDrive\Documents\Python\CollectionGeneration.py", line 70, in <module>
    n = np.random.randint(1, 5)
  File "mtrand.pyx", line 746, in numpy.random.mtrand.RandomState.randint
  File "_bounded_integers.pyx", line 1322, in numpy.random._bounded_integers._rand_int32
TypeError: 'tuple' object is not callable

I suspected the two events to be independent and I'm struggling to understand why this would be an issue. Perhaps even stranger, I can't replicate the issue when trying the rand or uniform functions.

This runs fine

import numpy as np
import math as ma

minradius = 0.1
maxradius = 0.9
maxlcm = 100

n = np.random.randint(1, 5)

a = np.linspace(int(minradius * 100), int(maxradius * 100), int((maxradius - minradius) * 100   1))
b = np.zeros((2, 1))

for i in a:
    for j in a:
        if ma.sqrt(i / 100)   0.09 < ma.sqrt(j / 100):
            if ma.lcm(int(i), int(j)) <= maxlcm:
                bn = np.array = ([int(i)], [int(j)])
                b = np.hstack((b, bn))

This does not

import numpy as np
import math as ma

minradius = 0.1
maxradius = 0.9
maxlcm = 100

a = np.linspace(int(minradius * 100), int(maxradius * 100), int((maxradius - minradius) * 100   1))
b = np.zeros((2, 1))

for i in a:
    for j in a:
        if ma.sqrt(i / 100)   0.09 < ma.sqrt(j / 100):
            if ma.lcm(int(i), int(j)) <= maxlcm:
                bn = np.array = ([int(i)], [int(j)])
                b = np.hstack((b, bn))

n = np.random.randint(1, 5)

Any advice would be appreciated and I'll try to provide more information if required.

CodePudding user response:

You're assigning np.array to a tuple on this line:

bn = np.array = ([int(i)], [int(j)])

I think you mean:

bn = np.array([int(i)], [int(j)])

The function np.random.randint is most likely calling np.array after you assign it to a tuple in your second example which creates the exception you see in your question.

  • Related