how to solve Error of list index out of range?
Question:
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 !!
Answers:
Your list T
is empty because it is defined by T = []
. You need to T.append(...)
something in order to index it later.
Inside your for loop, you are trying to check an element that doesn’t exist in the list.
You have two issues:
issue 1
for i in range(1,N):
^----0
issue 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
There are a few problems here;
- You’ve created an empty array
T = []
and tried to use its contents in the "for" loop. You need to assign some values into the array in order for it to not be empty and you can use it.
- You’ve created three variables named N,NP,NN and assigned values into them by taking inputs from the user. And then you assign zeros in both NP and NN, then what is the point in taking inputs there?
- And last but not least, you need to work on the
print()
function. In the line int(print('les valeurs négatives est ', NN))
, you try to convert the return value from print, which would have nothing to do with anything here. Did you try to write something like: print('les valeurs positives est ', int(NP))
? Here, it prints out the string which you provided 'les valeurs négatives est '
, and then prints out NP but it is converted to integer type.
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 !!
Your list T
is empty because it is defined by T = []
. You need to T.append(...)
something in order to index it later.
Inside your for loop, you are trying to check an element that doesn’t exist in the list.
You have two issues:
issue 1
for i in range(1,N):
^----0
issue 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
There are a few problems here;
- You’ve created an empty array
T = []
and tried to use its contents in the "for" loop. You need to assign some values into the array in order for it to not be empty and you can use it. - You’ve created three variables named N,NP,NN and assigned values into them by taking inputs from the user. And then you assign zeros in both NP and NN, then what is the point in taking inputs there?
- And last but not least, you need to work on the
print()
function. In the lineint(print('les valeurs négatives est ', NN))
, you try to convert the return value from print, which would have nothing to do with anything here. Did you try to write something like:print('les valeurs positives est ', int(NP))
? Here, it prints out the string which you provided'les valeurs négatives est '
, and then prints out NP but it is converted to integer type.