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.










share|improve this question




























    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.










    share|improve this question


























      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.










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 at 12:24

























      asked Nov 19 at 12:14









      Arvina Kori

      447




      447
























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






          share|improve this answer























          • 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


















          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)






          share|improve this answer





















          • It is performing the else block & giving a loop of "No Found" string.
            – Arvina Kori
            Nov 19 at 12:20


















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





          share|improve this answer




























            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





            share|improve this answer




























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





              share|improve this answer






























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





                share|improve this answer








                New contributor




                zxy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.

























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





                  share|improve this answer






























                    up vote
                    0
                    down vote













                    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






                    share|improve this answer























                    • 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 breaking.
                      – timgeb
                      Nov 19 at 12:35








                    • 1




                      thanks alot. updated !
                      – Usman
                      Nov 19 at 12:54


















                    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





                    share|improve this answer










                    New contributor




                    Danish Hassan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.

























                      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.






                      share|improve this answer










                      New contributor




                      j.dings is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.


















                      • 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











                      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',
                      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
                      });


                      }
                      });














                       

                      draft saved


                      draft discarded


















                      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








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






                      share|improve this answer























                      • 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















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






                      share|improve this answer























                      • 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













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






                      share|improve this answer














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







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      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


















                      • 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












                      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)






                      share|improve this answer





















                      • It is performing the else block & giving a loop of "No Found" string.
                        – Arvina Kori
                        Nov 19 at 12:20















                      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)






                      share|improve this answer





















                      • It is performing the else block & giving a loop of "No Found" string.
                        – Arvina Kori
                        Nov 19 at 12:20













                      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)






                      share|improve this answer












                      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)







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      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


















                      • 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










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





                      share|improve this answer

























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





                        share|improve this answer























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





                          share|improve this answer












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






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 19 at 12:28









                          Chirag

                          790210




                          790210






















                              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





                              share|improve this answer

























                                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





                                share|improve this answer























                                  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





                                  share|improve this answer












                                  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






                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Nov 19 at 12:29









                                  Harsha B

                                  2,50321632




                                  2,50321632






















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





                                      share|improve this answer



























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





                                        share|improve this answer

























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





                                          share|improve this answer














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






                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Nov 19 at 12:31

























                                          answered Nov 19 at 12:23









                                          Wolfeius

                                          13




                                          13






















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





                                              share|improve this answer








                                              New contributor




                                              zxy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                              Check out our Code of Conduct.






















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





                                                share|improve this answer








                                                New contributor




                                                zxy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                Check out our Code of Conduct.




















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





                                                  share|improve this answer








                                                  New contributor




                                                  zxy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                  Check out our Code of Conduct.









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






                                                  share|improve this answer








                                                  New contributor




                                                  zxy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                  Check out our Code of Conduct.









                                                  share|improve this answer



                                                  share|improve this answer






                                                  New contributor




                                                  zxy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                  Check out our Code of Conduct.









                                                  answered Nov 19 at 12:33









                                                  zxy

                                                  315




                                                  315




                                                  New contributor




                                                  zxy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                  Check out our Code of Conduct.





                                                  New contributor





                                                  zxy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                  Check out our Code of Conduct.






                                                  zxy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                  Check out our Code of Conduct.






















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





                                                      share|improve this answer



























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





                                                        share|improve this answer

























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





                                                          share|improve this answer














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






                                                          share|improve this answer














                                                          share|improve this answer



                                                          share|improve this answer








                                                          edited Nov 19 at 12:38

























                                                          answered Nov 19 at 12:29









                                                          Somya Avasthi

                                                          164




                                                          164






















                                                              up vote
                                                              0
                                                              down vote













                                                              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






                                                              share|improve this answer























                                                              • 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 breaking.
                                                                – timgeb
                                                                Nov 19 at 12:35








                                                              • 1




                                                                thanks alot. updated !
                                                                – Usman
                                                                Nov 19 at 12:54















                                                              up vote
                                                              0
                                                              down vote













                                                              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






                                                              share|improve this answer























                                                              • 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 breaking.
                                                                – timgeb
                                                                Nov 19 at 12:35








                                                              • 1




                                                                thanks alot. updated !
                                                                – Usman
                                                                Nov 19 at 12:54













                                                              up vote
                                                              0
                                                              down vote










                                                              up vote
                                                              0
                                                              down vote









                                                              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






                                                              share|improve this answer














                                                              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







                                                              share|improve this answer














                                                              share|improve this answer



                                                              share|improve this answer








                                                              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. The else block is executed at most once, if and only if the for loop runs to completion without ever breaking.
                                                                – 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 breaking.
                                                                – 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 breaking.
                                                              – 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 breaking.
                                                              – 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










                                                              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





                                                              share|improve this answer










                                                              New contributor




                                                              Danish Hassan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                              Check out our Code of Conduct.






















                                                                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





                                                                share|improve this answer










                                                                New contributor




                                                                Danish Hassan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                Check out our Code of Conduct.




















                                                                  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





                                                                  share|improve this answer










                                                                  New contributor




                                                                  Danish Hassan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                  Check out our Code of Conduct.









                                                                  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






                                                                  share|improve this answer










                                                                  New contributor




                                                                  Danish Hassan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                  Check out our Code of Conduct.









                                                                  share|improve this answer



                                                                  share|improve this answer








                                                                  edited Nov 19 at 13:32









                                                                  LW001

                                                                  1,46641122




                                                                  1,46641122






                                                                  New contributor




                                                                  Danish Hassan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                  Check out our Code of Conduct.









                                                                  answered Nov 19 at 13:13









                                                                  Danish Hassan

                                                                  11




                                                                  11




                                                                  New contributor




                                                                  Danish Hassan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                  Check out our Code of Conduct.





                                                                  New contributor





                                                                  Danish Hassan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                  Check out our Code of Conduct.






                                                                  Danish Hassan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                  Check out our Code of Conduct.






















                                                                      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.






                                                                      share|improve this answer










                                                                      New contributor




                                                                      j.dings is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                      Check out our Code of Conduct.


















                                                                      • 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















                                                                      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.






                                                                      share|improve this answer










                                                                      New contributor




                                                                      j.dings is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                      Check out our Code of Conduct.


















                                                                      • 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













                                                                      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.






                                                                      share|improve this answer










                                                                      New contributor




                                                                      j.dings is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                      Check out our Code of Conduct.









                                                                      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.







                                                                      share|improve this answer










                                                                      New contributor




                                                                      j.dings is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                      Check out our Code of Conduct.









                                                                      share|improve this answer



                                                                      share|improve this answer








                                                                      edited Nov 19 at 14:06









                                                                      Nils Gudat

                                                                      2,08011433




                                                                      2,08011433






                                                                      New contributor




                                                                      j.dings is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                      Check out our Code of Conduct.









                                                                      answered Nov 19 at 12:19









                                                                      j.dings

                                                                      233




                                                                      233




                                                                      New contributor




                                                                      j.dings is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                      Check out our Code of Conduct.





                                                                      New contributor





                                                                      j.dings is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                      Check out our Code of Conduct.






                                                                      j.dings is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                      Check out our Code of Conduct.












                                                                      • 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




                                                                      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


















                                                                       

                                                                      draft saved


                                                                      draft discarded



















































                                                                       


                                                                      draft saved


                                                                      draft discarded














                                                                      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





















































                                                                      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







                                                                      Popular posts from this blog

                                                                      404 Error Contact Form 7 ajax form submitting

                                                                      How to know if a Active Directory user can login interactively

                                                                      Refactoring coordinates for Minecraft Pi buildings written in Python