How to get the index of list value and value too choosen by user input
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 |
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 |
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 '18 at 12:24
asked Nov 19 '18 at 12:14


Arvina Kori
447
447
add a comment |
add a comment |
10 Answers
10
active
oldest
votes
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 '18 at 12:23
@ArvinaKori ok, I covered that now.
– timgeb
Nov 19 '18 at 12:24
1
Thank you mate it worked.
– Arvina Kori
Nov 19 '18 at 12:26
1
Pythonic solution :)
– Abdul Niyas P M
Nov 19 '18 at 12:28
add a comment |
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 '18 at 12:20
add a comment |
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 |
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 |
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 |
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))
add a comment |
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 |
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 '18 at 12:35
1
thanks alot. updated !
– Usman
Nov 19 '18 at 12:54
add a comment |
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
add a comment |
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
.
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 '18 at 12:51
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
10 Answers
10
active
oldest
votes
10 Answers
10
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 '18 at 12:23
@ArvinaKori ok, I covered that now.
– timgeb
Nov 19 '18 at 12:24
1
Thank you mate it worked.
– Arvina Kori
Nov 19 '18 at 12:26
1
Pythonic solution :)
– Abdul Niyas P M
Nov 19 '18 at 12:28
add a comment |
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 '18 at 12:23
@ArvinaKori ok, I covered that now.
– timgeb
Nov 19 '18 at 12:24
1
Thank you mate it worked.
– Arvina Kori
Nov 19 '18 at 12:26
1
Pythonic solution :)
– Abdul Niyas P M
Nov 19 '18 at 12:28
add a comment |
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 '18 at 12:30
answered Nov 19 '18 at 12:19


timgeb
49.7k116390
49.7k116390
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 '18 at 12:23
@ArvinaKori ok, I covered that now.
– timgeb
Nov 19 '18 at 12:24
1
Thank you mate it worked.
– Arvina Kori
Nov 19 '18 at 12:26
1
Pythonic solution :)
– Abdul Niyas P M
Nov 19 '18 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 '18 at 12:23
@ArvinaKori ok, I covered that now.
– timgeb
Nov 19 '18 at 12:24
1
Thank you mate it worked.
– Arvina Kori
Nov 19 '18 at 12:26
1
Pythonic solution :)
– Abdul Niyas P M
Nov 19 '18 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 '18 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 '18 at 12:23
@ArvinaKori ok, I covered that now.
– timgeb
Nov 19 '18 at 12:24
@ArvinaKori ok, I covered that now.
– timgeb
Nov 19 '18 at 12:24
1
1
Thank you mate it worked.
– Arvina Kori
Nov 19 '18 at 12:26
Thank you mate it worked.
– Arvina Kori
Nov 19 '18 at 12:26
1
1
Pythonic solution :)
– Abdul Niyas P M
Nov 19 '18 at 12:28
Pythonic solution :)
– Abdul Niyas P M
Nov 19 '18 at 12:28
add a comment |
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 '18 at 12:20
add a comment |
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 '18 at 12:20
add a comment |
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 '18 at 12:17
has
761517
761517
It is performing the else block & giving a loop of "No Found" string.
– Arvina Kori
Nov 19 '18 at 12:20
add a comment |
It is performing the else block & giving a loop of "No Found" string.
– Arvina Kori
Nov 19 '18 at 12:20
It is performing the else block & giving a loop of "No Found" string.
– Arvina Kori
Nov 19 '18 at 12:20
It is performing the else block & giving a loop of "No Found" string.
– Arvina Kori
Nov 19 '18 at 12:20
add a comment |
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 |
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 |
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 '18 at 12:28


Srce Cde
1,136511
1,136511
add a comment |
add a comment |
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 |
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 |
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 '18 at 12:29
Harsha B
2,55521733
2,55521733
add a comment |
add a comment |
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 |
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 |
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 '18 at 12:31
answered Nov 19 '18 at 12:23


Wolfeius
13
13
add a comment |
add a comment |
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))
add a comment |
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))
add a comment |
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))
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))
answered Nov 19 '18 at 12:33


zxy
1316
1316
add a comment |
add a comment |
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 |
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 |
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 '18 at 12:38
answered Nov 19 '18 at 12:29
Somya Avasthi
665
665
add a comment |
add a comment |
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 '18 at 12:35
1
thanks alot. updated !
– Usman
Nov 19 '18 at 12:54
add a comment |
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 '18 at 12:35
1
thanks alot. updated !
– Usman
Nov 19 '18 at 12:54
add a comment |
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 '18 at 12:54
answered Nov 19 '18 at 12:29


Usman
744514
744514
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 '18 at 12:35
1
thanks alot. updated !
– Usman
Nov 19 '18 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 '18 at 12:35
1
thanks alot. updated !
– Usman
Nov 19 '18 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 '18 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 '18 at 12:35
1
1
thanks alot. updated !
– Usman
Nov 19 '18 at 12:54
thanks alot. updated !
– Usman
Nov 19 '18 at 12:54
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Nov 19 '18 at 13:32
LW001
1,50441122
1,50441122
answered Nov 19 '18 at 13:13
Danish Hassan
11
11
add a comment |
add a comment |
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
.
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 '18 at 12:51
add a comment |
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
.
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 '18 at 12:51
add a comment |
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
.
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
.
edited Nov 19 '18 at 14:06


Nils Gudat
2,09511433
2,09511433
answered Nov 19 '18 at 12:19
j.dings
253
253
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 '18 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 '18 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 '18 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 '18 at 12:51
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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