I need to change a variable (int) inside a loop, but I am not having success

I am trying to update a variable (Nped) inside a while loop in Spyder.
This variable defines the lenght of the arrays (Qsie_ocup_vetor and fe_ocup_vetor) in the loop.
On the first interaction (densidade=0.1), it works well, but on the second, it does not work and the following mensage appears: “TypeError: only size-1 arrays can be converted to Python scalars”.

It follows a part of my code:

Area=3
densidade=0.1
while (densidade<0.9):    
      
    # Date of the Gauss distribution 
        M_mean=718.2/9.81
        M_std=143.6/9.81
        fp_Mean=2.0
        fp_std=0.175
    
    # Initializing the variable
        Nped=0    
        Nped=int((Area*densidade))

    
    # Initializing the vector
        Qsie_ocup_vetor=np.zeros(Nped)
        fe_ocup_vetor=np.zeros(Nped)

    # Computing...
        for i in range (Nped):
           Qsie_ocup_vetor[i]= 'here apears an equation'
           fe_ocup_vetor[i]= 'here apears an equation'
           print(densidade,Qsie_ocup_vetor[i],fe_ocup_vetor[i])

    # End of the loop: updating the variable "densidade"
        densidade=densidade+0.1

Off of the top of my head (although not tested) is the issue not with:

Qsie_ocup_vetor[i]= 'here apears an equation'
fe_ocup_vetor[i]= 'here apears an equation'

… which is assigning string literals?

That said, I can’t see how this would run even a first loop??

Edit to add: I now think what you mean is to comment the two lines that I’ve highlighted, but I still feel that therein is the issue; that is to say that whatever said equations are, there’s an issue with on subsequent loops.

I am trying to update a variable (Nped) inside a while loop in
Spyder.

This may be an English grammar thing, but you do not appear to be
modifying Nped itself inside the loop.

This variable defines the lenght of the arrays (Qsie_ocup_vetor and fe_ocup_vetor) in the loop.
On the first interaction (densidade=0.1), it works well, but on the second, it does not work and the following mensage appears: “TypeError: only size-1 arrays can be converted to Python scalars”.

It it best to also include the full traceback text. It should show the
exactly line where the TypeError occurred.

I think Rob is looking at the right piece of code:

 # Initializing the vector
     Qsie_ocup_vetor=np.zeros(Nped)
     fe_ocup_vetor=np.zeros(Nped)

Here you have defined 2 size-Nped arrays, initially filled with
zeroes.

 # Computing...
     for i in range (Nped):
        Qsie_ocup_vetor[i]= 'here apears an equation'
        fe_ocup_vetor[i]= 'here apears an equation'
        print(densidade,Qsie_ocup_vetor[i],fe_ocup_vetor[i])

Here’s you’re trying to assign to an element of each array. The arrays
are arrays of zeroes. To take one of those lines:

 Qsie_ocup_vetor[i]= 'here apears an equation'

This expects to store that stuff from the right hand side in the element
at [i] on the left, which accepts a number.

Can you show us a real example of what you would have at here apears an
equation
please?

I’m expecting that it is some kind or array itself, or some other
complex object. The error is complaining that numpy cannot store that
kind of thing in an array element which should hold a number.

Cheers,
Cameron Simpson cs@cskk.id.au

As Rob42 I can’t see how for-loop can run:

>>> import numpy as np
>>> Area=3
>>> densidade=0.1
>>> Nped = int((Area*densidade))
>>> Nped
0
>>> arr = np.zeros(Nped)
>>> arr
array([], dtype=float64)
>>> for i in range(Nped):
...     print('spam')
...
>>>