-2

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.

Arvina Kori
  • 93
  • 13

11 Answers11

1

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)).

timgeb
  • 76,762
  • 20
  • 123
  • 145
0

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)

Oli
  • 1,313
  • 14
  • 31
0

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.

Nils Gudat
  • 13,222
  • 3
  • 39
  • 60
j.dings
  • 45
  • 4
  • 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
0

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()
Wolfeius
  • 303
  • 2
  • 14
0
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")
Srce Cde
  • 1,764
  • 10
  • 15
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
Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
0

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")
0

There are some logical mistakes in the code :

  1. 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.

  2. 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 :

enter image description here

Usman
  • 1,983
  • 15
  • 28
  • 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
0

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))
Alif Jahan
  • 793
  • 1
  • 7
  • 20
0

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 
LW001
  • 2,452
  • 6
  • 27
  • 36
0

no need to use for loop to search for one type of num, You can use a "Conditional Statement".

search algorithm using if statement

a = [10, 20, 30, 40, 50, 60]

inp = int(input("Enter digit"))
if inp in a:
    index = a.index(inp)
    print(f"You found it, {inp} located in list on index {index}")
else:
    print("Not found")
M Z
  • 4,571
  • 2
  • 13
  • 27
Gaga
  • 1
  • 3
  • Sure there is no explicit for loop, but this is two times less efficient as `in` must read the list a first time, and `a.index` a second time. – mozway Apr 23 '23 at 20:43