[Python] Comparing array elements using for and if statement

Discussão em 'Python' iniciado por Stack, Maio 28, 2025.

  1. Stack

    Stack Membro Participativo

    I have the following code:

    import math
    import numpy
    import numpy as np
    import numpydoc
    import matplotlib
    x= [1,2 ,3 ,4 ,5 ,6]
    y= [5,6,7 ,8 ,9 ,10 ]

    ax=np.array(x)
    ay=np.array(y)
    for i in ax:
    if ax>4:
    print("Hi")
    else:
    print("Hello")


    I am getting this error:


    IndexError: index 6 is out of bounds for axis 0 with size 6
    ​

    I want the code to check each value of the array and if the condition in the if statement is satisfied, then the program runs/

    Continue reading...

Compartilhe esta Página