[Python] Not getting IndexError for negative index [duplicate]

Discussão em 'Python' iniciado por Stack, Setembro 8, 2025.

  1. Stack

    Stack Membro Participativo

    I have list of inputs called in_seq which is the input

    in_seq = [8, 2, 4, -3, 1, -5, 4, 7, 0, 2, 1, -6, -1, 0, 2, 1, 9, 6, -4, 5]


    a function called discsys_b which is the discrete-time system

    def discsys_b(x, n):
    # if index error occurs the value of the shifted value is 0
    try:
    return round(x[n]/3 + (7 * x[n - 6]),2)
    except IndexError:
    return round((1/3) * x[n] + 0,2)


    and a list called y_b which will be the output of input sequence computed within the discrete-time system

    y_b = [discsys_b(in_seq, n) for n in range(len(in_seq))]


    The first element of the output should be 2.67 because the 8 will be accessed multiplied by 1/3 and added with 0 since the except block will be executed. But it outputs 16.67.

    Continue reading...

Compartilhe esta Página