How to get the index of list value and value too choosen by user input
up vote
-2
down vote
favorite
I want that a user can enter digits and if the digits matches with the list values then the program returns the entered value and it's index too.
How can i do that?
I have written some line of code but it is not working....
a = [10, 20, 30, 40, 50, 60]
inp = int(input("Enter digit"))
i =0
for i in a:
if inp == a[i]:
print("You found it {}".format(a[i]))
else:
print("No found")
It is raising an IndexError.
python python-3.x error-handling
add a comment |
up vote
-2
down vote
favorite
I want that a user can enter digits and if the digits matches with the list values then the program returns the entered value and it's index too.
How can i do that?
I have written some line of code but it is not working....
a = [10, 20, 30, 40, 50, 60]
inp = int(input("Enter digit"))
i =0
for i in a:
if inp == a[i]:
print("You found it {}".format(a[i]))
else:
print("No found")
It is raising an IndexError.
python python-3.x error-handling
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I want that a user can enter digits and if the digits matches with the list values then the program returns the entered value and it's index too.
How can i do that?
I have written some line of code but it is not working....
a = [10, 20, 30, 40, 50, 60]
inp = int(input("Enter digit"))
i =0
for i in a:
if inp == a[i]:
print("You found it {}".format(a[i]))
else:
print("No found")
It is raising an IndexError.
python python-3.x error-handling
I want that a user can enter digits and if the digits matches with the list values then the program returns the entered value and it's index too.
How can i do that?
I have written some line of code but it is not working....
a = [10, 20, 30, 40, 50, 60]
inp = int(input("Enter digit"))
i =0
for i in a:
if inp == a[i]:
print("You found it {}".format(a[i]))
else:
print("No found")
It is raising an IndexError.
python python-3.x error-handling
python python-3.x error-handling
edited Nov 19 at 12:24
asked Nov 19 at 12:14
Arvina Kori
447
447
add a comment |
add a comment |
10 Answers
10
active
oldest
votes
up vote
1
down vote
accepted
for i in a
iterates over the elements of a
, not over its integer indices.
You can fix your code by using enumerate
.
a = [10, 20, 30, 40, 50, 60]
inp = int(input('Enter digit: '))
for index, value in enumerate(a):
if value == inp:
print('You found it at position {}'.format(index))
break
else: # no break
print('not found')
Additionally, I changed the string input('Enter digit: ')
returns to an int
and break
out of the loop once the target has been seen once.
See this question for solutions how to program this behavior outside of a programming exercise (TL;DR: a.index(inp)
).
Yes this is giving 'You found it' String but i want the index of entered value. I edited with print('You found it {}'.format(value)) but it is giving the value not index
– Arvina Kori
Nov 19 at 12:23
@ArvinaKori ok, I covered that now.
– timgeb
Nov 19 at 12:24
1
Thank you mate it worked.
– Arvina Kori
Nov 19 at 12:26
1
Pythonic solution :)
– Abdul Niyas P M
Nov 19 at 12:28
add a comment |
up vote
0
down vote
Change
for i in a:
if inp == a[i]:
...
to
for i in a:
if inp == i:
...
Because for
loop iterate over elements in list.(not over index)
It is performing the else block & giving a loop of "No Found" string.
– Arvina Kori
Nov 19 at 12:20
add a comment |
up vote
0
down vote
a = [10, 20, 30, 40, 50, 60]
inp = int(input("Enter digit: "))
if inp in a:
print("You found {} at {}".format(inp, a.index(inp)))
else:
print("Not found")
add a comment |
up vote
0
down vote
More pythonic way would be:
a = [10, 20, 30, 40, 50, 60]
inp = int(input("Enter digit :"))
if inp in a:
print "You found", inp, "at index:", a.index(inp)
else:
print "Not found"
Output:
Enter digit :10
You found 10 at index: 0
add a comment |
up vote
0
down vote
I don't think a for loop is necessary:
l = [i for i in range(0,100,10)]
def found_it():
print("You found it {}".format(l.index(ans)))
if ans in l:
print("You found it")
else:
print("Try again")
found_it()
add a comment |
up vote
0
down vote
You can use in
to check it in the list
lst = [11, 1, 4, 15, 6]
userInput = int(input('Enter Value')) # IF IT IS INT
if userInput in lst:
print(userInput, lst.index(userInput))
New contributor
add a comment |
up vote
0
down vote
Firstly,always convert the user input into an integer using the int() function, before finding it in the list of integers.
I guess you only need to find whether one element is there in list or not.For that you can try this:
a = [10, 20, 30, 40, 50, 60]
inp = int(input("Enter digit"))
if inp in a:
print("You found it at index {}".format(a.index(inp)))
else:
print("Not found")
add a comment |
up vote
0
down vote
There are some logical mistakes in the code :
The input function return the string value & you are comparing the
string value 'inp' with the array int value. Need to type-cast the
value.for i in a (means you are accessing each value of array 'a' in the
variable 'i'). Here 'i' is not use as a index variable. Need to
update it with 'for i in range(0,len(a))' (which means iterate the
for-loop till the length of array 'a' with the index value of 'i').
Code :
a = [10, 20, 30, 40, 50, 60]
flag = 0
inp = int(input("Enter digit"))
for i in range(0,len(a)):
if inp == a[i]:
print("You found it")
print("index = ", i)
flag = 1
break
if flag == 0:
print("No found")
Output :
On point 3:for
/else
is a valid Python construct. Theelse
block is executed at most once, if and only if thefor
loop runs to completion without everbreak
ing.
– timgeb
Nov 19 at 12:35
1
thanks alot. updated !
– Usman
Nov 19 at 12:54
add a comment |
up vote
0
down vote
You are facing this problem because in for loop i get the array values and storing in i that's why showing Index Error.
Here us the code to find Input no and matching no:
a=[10,20,30,40,50,60]
inp=20
i=0
for i in a:
c = i
print(c)
if inp==i:
print("You found it")
Code Output:
10
20
You found it
30
40
50
60
New contributor
add a comment |
up vote
0
down vote
You have to use
for i in range(len(a)):
The index error is raised by the fact that you construct your for
loop as for i in a
, which means that i
will iterate through the contents of a
. Then calling a[i]
will not work because in the first step of the loop you are calling a[10]
.
Alternatively, you could step through the elements of a
directly like this:
for a_element in a:
if inp == a_element:
Which will compare the user input to the contents of a
.
New contributor
I think it would be useful if you elaborated a bit. Where should this line be used? Why didn't the code in the question work?
– KenHBS
Nov 19 at 12:51
add a comment |
10 Answers
10
active
oldest
votes
10 Answers
10
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
for i in a
iterates over the elements of a
, not over its integer indices.
You can fix your code by using enumerate
.
a = [10, 20, 30, 40, 50, 60]
inp = int(input('Enter digit: '))
for index, value in enumerate(a):
if value == inp:
print('You found it at position {}'.format(index))
break
else: # no break
print('not found')
Additionally, I changed the string input('Enter digit: ')
returns to an int
and break
out of the loop once the target has been seen once.
See this question for solutions how to program this behavior outside of a programming exercise (TL;DR: a.index(inp)
).
Yes this is giving 'You found it' String but i want the index of entered value. I edited with print('You found it {}'.format(value)) but it is giving the value not index
– Arvina Kori
Nov 19 at 12:23
@ArvinaKori ok, I covered that now.
– timgeb
Nov 19 at 12:24
1
Thank you mate it worked.
– Arvina Kori
Nov 19 at 12:26
1
Pythonic solution :)
– Abdul Niyas P M
Nov 19 at 12:28
add a comment |
up vote
1
down vote
accepted
for i in a
iterates over the elements of a
, not over its integer indices.
You can fix your code by using enumerate
.
a = [10, 20, 30, 40, 50, 60]
inp = int(input('Enter digit: '))
for index, value in enumerate(a):
if value == inp:
print('You found it at position {}'.format(index))
break
else: # no break
print('not found')
Additionally, I changed the string input('Enter digit: ')
returns to an int
and break
out of the loop once the target has been seen once.
See this question for solutions how to program this behavior outside of a programming exercise (TL;DR: a.index(inp)
).
Yes this is giving 'You found it' String but i want the index of entered value. I edited with print('You found it {}'.format(value)) but it is giving the value not index
– Arvina Kori
Nov 19 at 12:23
@ArvinaKori ok, I covered that now.
– timgeb
Nov 19 at 12:24
1
Thank you mate it worked.
– Arvina Kori
Nov 19 at 12:26
1
Pythonic solution :)
– Abdul Niyas P M
Nov 19 at 12:28
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
for i in a
iterates over the elements of a
, not over its integer indices.
You can fix your code by using enumerate
.
a = [10, 20, 30, 40, 50, 60]
inp = int(input('Enter digit: '))
for index, value in enumerate(a):
if value == inp:
print('You found it at position {}'.format(index))
break
else: # no break
print('not found')
Additionally, I changed the string input('Enter digit: ')
returns to an int
and break
out of the loop once the target has been seen once.
See this question for solutions how to program this behavior outside of a programming exercise (TL;DR: a.index(inp)
).
for i in a
iterates over the elements of a
, not over its integer indices.
You can fix your code by using enumerate
.
a = [10, 20, 30, 40, 50, 60]
inp = int(input('Enter digit: '))
for index, value in enumerate(a):
if value == inp:
print('You found it at position {}'.format(index))
break
else: # no break
print('not found')
Additionally, I changed the string input('Enter digit: ')
returns to an int
and break
out of the loop once the target has been seen once.
See this question for solutions how to program this behavior outside of a programming exercise (TL;DR: a.index(inp)
).
edited Nov 19 at 12:30
answered Nov 19 at 12:19
timgeb
45.1k106286
45.1k106286
Yes this is giving 'You found it' String but i want the index of entered value. I edited with print('You found it {}'.format(value)) but it is giving the value not index
– Arvina Kori
Nov 19 at 12:23
@ArvinaKori ok, I covered that now.
– timgeb
Nov 19 at 12:24
1
Thank you mate it worked.
– Arvina Kori
Nov 19 at 12:26
1
Pythonic solution :)
– Abdul Niyas P M
Nov 19 at 12:28
add a comment |
Yes this is giving 'You found it' String but i want the index of entered value. I edited with print('You found it {}'.format(value)) but it is giving the value not index
– Arvina Kori
Nov 19 at 12:23
@ArvinaKori ok, I covered that now.
– timgeb
Nov 19 at 12:24
1
Thank you mate it worked.
– Arvina Kori
Nov 19 at 12:26
1
Pythonic solution :)
– Abdul Niyas P M
Nov 19 at 12:28
Yes this is giving 'You found it' String but i want the index of entered value. I edited with print('You found it {}'.format(value)) but it is giving the value not index
– Arvina Kori
Nov 19 at 12:23
Yes this is giving 'You found it' String but i want the index of entered value. I edited with print('You found it {}'.format(value)) but it is giving the value not index
– Arvina Kori
Nov 19 at 12:23
@ArvinaKori ok, I covered that now.
– timgeb
Nov 19 at 12:24
@ArvinaKori ok, I covered that now.
– timgeb
Nov 19 at 12:24
1
1
Thank you mate it worked.
– Arvina Kori
Nov 19 at 12:26
Thank you mate it worked.
– Arvina Kori
Nov 19 at 12:26
1
1
Pythonic solution :)
– Abdul Niyas P M
Nov 19 at 12:28
Pythonic solution :)
– Abdul Niyas P M
Nov 19 at 12:28
add a comment |
up vote
0
down vote
Change
for i in a:
if inp == a[i]:
...
to
for i in a:
if inp == i:
...
Because for
loop iterate over elements in list.(not over index)
It is performing the else block & giving a loop of "No Found" string.
– Arvina Kori
Nov 19 at 12:20
add a comment |
up vote
0
down vote
Change
for i in a:
if inp == a[i]:
...
to
for i in a:
if inp == i:
...
Because for
loop iterate over elements in list.(not over index)
It is performing the else block & giving a loop of "No Found" string.
– Arvina Kori
Nov 19 at 12:20
add a comment |
up vote
0
down vote
up vote
0
down vote
Change
for i in a:
if inp == a[i]:
...
to
for i in a:
if inp == i:
...
Because for
loop iterate over elements in list.(not over index)
Change
for i in a:
if inp == a[i]:
...
to
for i in a:
if inp == i:
...
Because for
loop iterate over elements in list.(not over index)
answered Nov 19 at 12:17
has
696517
696517
It is performing the else block & giving a loop of "No Found" string.
– Arvina Kori
Nov 19 at 12:20
add a comment |
It is performing the else block & giving a loop of "No Found" string.
– Arvina Kori
Nov 19 at 12:20
It is performing the else block & giving a loop of "No Found" string.
– Arvina Kori
Nov 19 at 12:20
It is performing the else block & giving a loop of "No Found" string.
– Arvina Kori
Nov 19 at 12:20
add a comment |
up vote
0
down vote
a = [10, 20, 30, 40, 50, 60]
inp = int(input("Enter digit: "))
if inp in a:
print("You found {} at {}".format(inp, a.index(inp)))
else:
print("Not found")
add a comment |
up vote
0
down vote
a = [10, 20, 30, 40, 50, 60]
inp = int(input("Enter digit: "))
if inp in a:
print("You found {} at {}".format(inp, a.index(inp)))
else:
print("Not found")
add a comment |
up vote
0
down vote
up vote
0
down vote
a = [10, 20, 30, 40, 50, 60]
inp = int(input("Enter digit: "))
if inp in a:
print("You found {} at {}".format(inp, a.index(inp)))
else:
print("Not found")
a = [10, 20, 30, 40, 50, 60]
inp = int(input("Enter digit: "))
if inp in a:
print("You found {} at {}".format(inp, a.index(inp)))
else:
print("Not found")
answered Nov 19 at 12:28
Chirag
790210
790210
add a comment |
add a comment |
up vote
0
down vote
More pythonic way would be:
a = [10, 20, 30, 40, 50, 60]
inp = int(input("Enter digit :"))
if inp in a:
print "You found", inp, "at index:", a.index(inp)
else:
print "Not found"
Output:
Enter digit :10
You found 10 at index: 0
add a comment |
up vote
0
down vote
More pythonic way would be:
a = [10, 20, 30, 40, 50, 60]
inp = int(input("Enter digit :"))
if inp in a:
print "You found", inp, "at index:", a.index(inp)
else:
print "Not found"
Output:
Enter digit :10
You found 10 at index: 0
add a comment |
up vote
0
down vote
up vote
0
down vote
More pythonic way would be:
a = [10, 20, 30, 40, 50, 60]
inp = int(input("Enter digit :"))
if inp in a:
print "You found", inp, "at index:", a.index(inp)
else:
print "Not found"
Output:
Enter digit :10
You found 10 at index: 0
More pythonic way would be:
a = [10, 20, 30, 40, 50, 60]
inp = int(input("Enter digit :"))
if inp in a:
print "You found", inp, "at index:", a.index(inp)
else:
print "Not found"
Output:
Enter digit :10
You found 10 at index: 0
answered Nov 19 at 12:29
Harsha B
2,50321632
2,50321632
add a comment |
add a comment |
up vote
0
down vote
I don't think a for loop is necessary:
l = [i for i in range(0,100,10)]
def found_it():
print("You found it {}".format(l.index(ans)))
if ans in l:
print("You found it")
else:
print("Try again")
found_it()
add a comment |
up vote
0
down vote
I don't think a for loop is necessary:
l = [i for i in range(0,100,10)]
def found_it():
print("You found it {}".format(l.index(ans)))
if ans in l:
print("You found it")
else:
print("Try again")
found_it()
add a comment |
up vote
0
down vote
up vote
0
down vote
I don't think a for loop is necessary:
l = [i for i in range(0,100,10)]
def found_it():
print("You found it {}".format(l.index(ans)))
if ans in l:
print("You found it")
else:
print("Try again")
found_it()
I don't think a for loop is necessary:
l = [i for i in range(0,100,10)]
def found_it():
print("You found it {}".format(l.index(ans)))
if ans in l:
print("You found it")
else:
print("Try again")
found_it()
edited Nov 19 at 12:31
answered Nov 19 at 12:23
Wolfeius
13
13
add a comment |
add a comment |
up vote
0
down vote
You can use in
to check it in the list
lst = [11, 1, 4, 15, 6]
userInput = int(input('Enter Value')) # IF IT IS INT
if userInput in lst:
print(userInput, lst.index(userInput))
New contributor
add a comment |
up vote
0
down vote
You can use in
to check it in the list
lst = [11, 1, 4, 15, 6]
userInput = int(input('Enter Value')) # IF IT IS INT
if userInput in lst:
print(userInput, lst.index(userInput))
New contributor
add a comment |
up vote
0
down vote
up vote
0
down vote
You can use in
to check it in the list
lst = [11, 1, 4, 15, 6]
userInput = int(input('Enter Value')) # IF IT IS INT
if userInput in lst:
print(userInput, lst.index(userInput))
New contributor
You can use in
to check it in the list
lst = [11, 1, 4, 15, 6]
userInput = int(input('Enter Value')) # IF IT IS INT
if userInput in lst:
print(userInput, lst.index(userInput))
New contributor
New contributor
answered Nov 19 at 12:33
zxy
315
315
New contributor
New contributor
add a comment |
add a comment |
up vote
0
down vote
Firstly,always convert the user input into an integer using the int() function, before finding it in the list of integers.
I guess you only need to find whether one element is there in list or not.For that you can try this:
a = [10, 20, 30, 40, 50, 60]
inp = int(input("Enter digit"))
if inp in a:
print("You found it at index {}".format(a.index(inp)))
else:
print("Not found")
add a comment |
up vote
0
down vote
Firstly,always convert the user input into an integer using the int() function, before finding it in the list of integers.
I guess you only need to find whether one element is there in list or not.For that you can try this:
a = [10, 20, 30, 40, 50, 60]
inp = int(input("Enter digit"))
if inp in a:
print("You found it at index {}".format(a.index(inp)))
else:
print("Not found")
add a comment |
up vote
0
down vote
up vote
0
down vote
Firstly,always convert the user input into an integer using the int() function, before finding it in the list of integers.
I guess you only need to find whether one element is there in list or not.For that you can try this:
a = [10, 20, 30, 40, 50, 60]
inp = int(input("Enter digit"))
if inp in a:
print("You found it at index {}".format(a.index(inp)))
else:
print("Not found")
Firstly,always convert the user input into an integer using the int() function, before finding it in the list of integers.
I guess you only need to find whether one element is there in list or not.For that you can try this:
a = [10, 20, 30, 40, 50, 60]
inp = int(input("Enter digit"))
if inp in a:
print("You found it at index {}".format(a.index(inp)))
else:
print("Not found")
edited Nov 19 at 12:38
answered Nov 19 at 12:29
Somya Avasthi
164
164
add a comment |
add a comment |
up vote
0
down vote
There are some logical mistakes in the code :
The input function return the string value & you are comparing the
string value 'inp' with the array int value. Need to type-cast the
value.for i in a (means you are accessing each value of array 'a' in the
variable 'i'). Here 'i' is not use as a index variable. Need to
update it with 'for i in range(0,len(a))' (which means iterate the
for-loop till the length of array 'a' with the index value of 'i').
Code :
a = [10, 20, 30, 40, 50, 60]
flag = 0
inp = int(input("Enter digit"))
for i in range(0,len(a)):
if inp == a[i]:
print("You found it")
print("index = ", i)
flag = 1
break
if flag == 0:
print("No found")
Output :
On point 3:for
/else
is a valid Python construct. Theelse
block is executed at most once, if and only if thefor
loop runs to completion without everbreak
ing.
– timgeb
Nov 19 at 12:35
1
thanks alot. updated !
– Usman
Nov 19 at 12:54
add a comment |
up vote
0
down vote
There are some logical mistakes in the code :
The input function return the string value & you are comparing the
string value 'inp' with the array int value. Need to type-cast the
value.for i in a (means you are accessing each value of array 'a' in the
variable 'i'). Here 'i' is not use as a index variable. Need to
update it with 'for i in range(0,len(a))' (which means iterate the
for-loop till the length of array 'a' with the index value of 'i').
Code :
a = [10, 20, 30, 40, 50, 60]
flag = 0
inp = int(input("Enter digit"))
for i in range(0,len(a)):
if inp == a[i]:
print("You found it")
print("index = ", i)
flag = 1
break
if flag == 0:
print("No found")
Output :
On point 3:for
/else
is a valid Python construct. Theelse
block is executed at most once, if and only if thefor
loop runs to completion without everbreak
ing.
– timgeb
Nov 19 at 12:35
1
thanks alot. updated !
– Usman
Nov 19 at 12:54
add a comment |
up vote
0
down vote
up vote
0
down vote
There are some logical mistakes in the code :
The input function return the string value & you are comparing the
string value 'inp' with the array int value. Need to type-cast the
value.for i in a (means you are accessing each value of array 'a' in the
variable 'i'). Here 'i' is not use as a index variable. Need to
update it with 'for i in range(0,len(a))' (which means iterate the
for-loop till the length of array 'a' with the index value of 'i').
Code :
a = [10, 20, 30, 40, 50, 60]
flag = 0
inp = int(input("Enter digit"))
for i in range(0,len(a)):
if inp == a[i]:
print("You found it")
print("index = ", i)
flag = 1
break
if flag == 0:
print("No found")
Output :
There are some logical mistakes in the code :
The input function return the string value & you are comparing the
string value 'inp' with the array int value. Need to type-cast the
value.for i in a (means you are accessing each value of array 'a' in the
variable 'i'). Here 'i' is not use as a index variable. Need to
update it with 'for i in range(0,len(a))' (which means iterate the
for-loop till the length of array 'a' with the index value of 'i').
Code :
a = [10, 20, 30, 40, 50, 60]
flag = 0
inp = int(input("Enter digit"))
for i in range(0,len(a)):
if inp == a[i]:
print("You found it")
print("index = ", i)
flag = 1
break
if flag == 0:
print("No found")
Output :
edited Nov 19 at 12:54
answered Nov 19 at 12:29
Usman
674413
674413
On point 3:for
/else
is a valid Python construct. Theelse
block is executed at most once, if and only if thefor
loop runs to completion without everbreak
ing.
– timgeb
Nov 19 at 12:35
1
thanks alot. updated !
– Usman
Nov 19 at 12:54
add a comment |
On point 3:for
/else
is a valid Python construct. Theelse
block is executed at most once, if and only if thefor
loop runs to completion without everbreak
ing.
– timgeb
Nov 19 at 12:35
1
thanks alot. updated !
– Usman
Nov 19 at 12:54
On point 3:
for
/else
is a valid Python construct. The else
block is executed at most once, if and only if the for
loop runs to completion without ever break
ing.– timgeb
Nov 19 at 12:35
On point 3:
for
/else
is a valid Python construct. The else
block is executed at most once, if and only if the for
loop runs to completion without ever break
ing.– timgeb
Nov 19 at 12:35
1
1
thanks alot. updated !
– Usman
Nov 19 at 12:54
thanks alot. updated !
– Usman
Nov 19 at 12:54
add a comment |
up vote
0
down vote
You are facing this problem because in for loop i get the array values and storing in i that's why showing Index Error.
Here us the code to find Input no and matching no:
a=[10,20,30,40,50,60]
inp=20
i=0
for i in a:
c = i
print(c)
if inp==i:
print("You found it")
Code Output:
10
20
You found it
30
40
50
60
New contributor
add a comment |
up vote
0
down vote
You are facing this problem because in for loop i get the array values and storing in i that's why showing Index Error.
Here us the code to find Input no and matching no:
a=[10,20,30,40,50,60]
inp=20
i=0
for i in a:
c = i
print(c)
if inp==i:
print("You found it")
Code Output:
10
20
You found it
30
40
50
60
New contributor
add a comment |
up vote
0
down vote
up vote
0
down vote
You are facing this problem because in for loop i get the array values and storing in i that's why showing Index Error.
Here us the code to find Input no and matching no:
a=[10,20,30,40,50,60]
inp=20
i=0
for i in a:
c = i
print(c)
if inp==i:
print("You found it")
Code Output:
10
20
You found it
30
40
50
60
New contributor
You are facing this problem because in for loop i get the array values and storing in i that's why showing Index Error.
Here us the code to find Input no and matching no:
a=[10,20,30,40,50,60]
inp=20
i=0
for i in a:
c = i
print(c)
if inp==i:
print("You found it")
Code Output:
10
20
You found it
30
40
50
60
New contributor
edited Nov 19 at 13:32
LW001
1,46641122
1,46641122
New contributor
answered Nov 19 at 13:13
Danish Hassan
11
11
New contributor
New contributor
add a comment |
add a comment |
up vote
0
down vote
You have to use
for i in range(len(a)):
The index error is raised by the fact that you construct your for
loop as for i in a
, which means that i
will iterate through the contents of a
. Then calling a[i]
will not work because in the first step of the loop you are calling a[10]
.
Alternatively, you could step through the elements of a
directly like this:
for a_element in a:
if inp == a_element:
Which will compare the user input to the contents of a
.
New contributor
I think it would be useful if you elaborated a bit. Where should this line be used? Why didn't the code in the question work?
– KenHBS
Nov 19 at 12:51
add a comment |
up vote
0
down vote
You have to use
for i in range(len(a)):
The index error is raised by the fact that you construct your for
loop as for i in a
, which means that i
will iterate through the contents of a
. Then calling a[i]
will not work because in the first step of the loop you are calling a[10]
.
Alternatively, you could step through the elements of a
directly like this:
for a_element in a:
if inp == a_element:
Which will compare the user input to the contents of a
.
New contributor
I think it would be useful if you elaborated a bit. Where should this line be used? Why didn't the code in the question work?
– KenHBS
Nov 19 at 12:51
add a comment |
up vote
0
down vote
up vote
0
down vote
You have to use
for i in range(len(a)):
The index error is raised by the fact that you construct your for
loop as for i in a
, which means that i
will iterate through the contents of a
. Then calling a[i]
will not work because in the first step of the loop you are calling a[10]
.
Alternatively, you could step through the elements of a
directly like this:
for a_element in a:
if inp == a_element:
Which will compare the user input to the contents of a
.
New contributor
You have to use
for i in range(len(a)):
The index error is raised by the fact that you construct your for
loop as for i in a
, which means that i
will iterate through the contents of a
. Then calling a[i]
will not work because in the first step of the loop you are calling a[10]
.
Alternatively, you could step through the elements of a
directly like this:
for a_element in a:
if inp == a_element:
Which will compare the user input to the contents of a
.
New contributor
edited Nov 19 at 14:06
Nils Gudat
2,08011433
2,08011433
New contributor
answered Nov 19 at 12:19
j.dings
233
233
New contributor
New contributor
I think it would be useful if you elaborated a bit. Where should this line be used? Why didn't the code in the question work?
– KenHBS
Nov 19 at 12:51
add a comment |
I think it would be useful if you elaborated a bit. Where should this line be used? Why didn't the code in the question work?
– KenHBS
Nov 19 at 12:51
I think it would be useful if you elaborated a bit. Where should this line be used? Why didn't the code in the question work?
– KenHBS
Nov 19 at 12:51
I think it would be useful if you elaborated a bit. Where should this line be used? Why didn't the code in the question work?
– KenHBS
Nov 19 at 12:51
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374424%2fhow-to-get-the-index-of-list-value-and-value-too-choosen-by-user-input%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown