Home > Back-end >  how to solve Error of list index out of range?
how to solve Error of list index out of range?

Time:10-15

I have a problem, I write positive and negative numbers program.
I have no idea how to solve it... Here is my code: code:

T = []
N=int(input('Entre un nombre '))
NP=int(input('Entre un nombre positif '))
NN=int(input('Entre un nombre négatif '))
NP=0
NN=0
for i in range(1,N):
    if (T[i] <0 ):
        NN=NN 1
    else:
         NP=NP 1
int(print('les valeurs positives est ', NP))
int(print('les valeurs négatives est ', NN))  

Help Me Please !!

CodePudding user response:

isue 1

for i in range(1,N):
               ^----0

isue 2

int(print('les valeurs positives est ', NP))
^^^----------------------------------------^

1 - Start with 0, since list in python are zero-based:
2 - int it's a function convert characters into numbers

CodePudding user response:

Your list T is empty because it is defined by T = []. You need to T.append(...) something in order to index it later.

CodePudding user response:

Inside your for loop, you are trying to check an element that doesn't exist in the list.

  • Related