How to get the index of list value and value too choosen by user input












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










share|improve this question





























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










    share|improve this question



























      -2












      -2








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










      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 '18 at 12:24

























      asked Nov 19 '18 at 12:14









      Arvina Kori

      447




      447
























          10 Answers
          10






          active

          oldest

          votes


















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






          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 '18 at 12:23












          • @ArvinaKori ok, I covered that now.
            – timgeb
            Nov 19 '18 at 12:24






          • 1




            Thank you mate it worked.
            – Arvina Kori
            Nov 19 '18 at 12:26






          • 1




            Pythonic solution :)
            – Abdul Niyas P M
            Nov 19 '18 at 12:28



















          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)






          share|improve this answer





















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



















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





          share|improve this answer





























            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





            share|improve this answer





























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





              share|improve this answer































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





                share|improve this answer





























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





                  share|improve this answer































                    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






                    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 '18 at 12:35








                    • 1




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



















                    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





                    share|improve this answer































                      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.






                      share|improve this answer























                      • 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











                      Your Answer






                      StackExchange.ifUsing("editor", function () {
                      StackExchange.using("externalEditor", function () {
                      StackExchange.using("snippets", function () {
                      StackExchange.snippets.init();
                      });
                      });
                      }, "code-snippets");

                      StackExchange.ready(function() {
                      var channelOptions = {
                      tags: "".split(" "),
                      id: "1"
                      };
                      initTagRenderer("".split(" "), "".split(" "), channelOptions);

                      StackExchange.using("externalEditor", function() {
                      // Have to fire editor after snippets, if snippets enabled
                      if (StackExchange.settings.snippets.snippetsEnabled) {
                      StackExchange.using("snippets", function() {
                      createEditor();
                      });
                      }
                      else {
                      createEditor();
                      }
                      });

                      function createEditor() {
                      StackExchange.prepareEditor({
                      heartbeatType: 'answer',
                      autoActivateHeartbeat: false,
                      convertImagesToLinks: true,
                      noModals: true,
                      showLowRepImageUploadWarning: true,
                      reputationToPostImages: 10,
                      bindNavPrevention: true,
                      postfix: "",
                      imageUploader: {
                      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                      allowUrls: true
                      },
                      onDemand: true,
                      discardSelector: ".discard-answer"
                      ,immediatelyShowMarkdownHelp:true
                      });


                      }
                      });














                      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









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






                      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 '18 at 12:23












                      • @ArvinaKori ok, I covered that now.
                        – timgeb
                        Nov 19 '18 at 12:24






                      • 1




                        Thank you mate it worked.
                        – Arvina Kori
                        Nov 19 '18 at 12:26






                      • 1




                        Pythonic solution :)
                        – Abdul Niyas P M
                        Nov 19 '18 at 12:28
















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






                      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 '18 at 12:23












                      • @ArvinaKori ok, I covered that now.
                        – timgeb
                        Nov 19 '18 at 12:24






                      • 1




                        Thank you mate it worked.
                        – Arvina Kori
                        Nov 19 '18 at 12:26






                      • 1




                        Pythonic solution :)
                        – Abdul Niyas P M
                        Nov 19 '18 at 12:28














                      1












                      1








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






                      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 '18 at 12:30

























                      answered Nov 19 '18 at 12:19









                      timgeb

                      49.7k116390




                      49.7k116390












                      • Yes this is giving 'You found it' String but i want the index of entered value. I edited with print('You found it {}'.format(value)) but it is giving the value not index
                        – Arvina Kori
                        Nov 19 '18 at 12:23












                      • @ArvinaKori ok, I covered that now.
                        – timgeb
                        Nov 19 '18 at 12:24






                      • 1




                        Thank you mate it worked.
                        – Arvina Kori
                        Nov 19 '18 at 12:26






                      • 1




                        Pythonic solution :)
                        – Abdul Niyas P M
                        Nov 19 '18 at 12:28


















                      • Yes this is giving 'You found it' String but i want the index of entered value. I edited with print('You found it {}'.format(value)) but it is giving the value not index
                        – Arvina Kori
                        Nov 19 '18 at 12:23












                      • @ArvinaKori ok, I covered that now.
                        – timgeb
                        Nov 19 '18 at 12:24






                      • 1




                        Thank you mate it worked.
                        – Arvina Kori
                        Nov 19 '18 at 12:26






                      • 1




                        Pythonic solution :)
                        – Abdul Niyas P M
                        Nov 19 '18 at 12:28
















                      Yes this is giving 'You found it' String but i want the index of entered value. I edited with print('You found it {}'.format(value)) but it is giving the value not index
                      – Arvina Kori
                      Nov 19 '18 at 12:23






                      Yes this is giving 'You found it' String but i want the index of entered value. I edited with print('You found it {}'.format(value)) but it is giving the value not index
                      – Arvina Kori
                      Nov 19 '18 at 12:23














                      @ArvinaKori ok, I covered that now.
                      – timgeb
                      Nov 19 '18 at 12:24




                      @ArvinaKori ok, I covered that now.
                      – timgeb
                      Nov 19 '18 at 12:24




                      1




                      1




                      Thank you mate it worked.
                      – Arvina Kori
                      Nov 19 '18 at 12:26




                      Thank you mate it worked.
                      – Arvina Kori
                      Nov 19 '18 at 12:26




                      1




                      1




                      Pythonic solution :)
                      – Abdul Niyas P M
                      Nov 19 '18 at 12:28




                      Pythonic solution :)
                      – Abdul Niyas P M
                      Nov 19 '18 at 12:28













                      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)






                      share|improve this answer





















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
















                      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)






                      share|improve this answer





















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














                      0












                      0








                      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)






                      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 '18 at 12:17









                      has

                      761517




                      761517












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


















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
















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




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











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





                      share|improve this answer


























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





                        share|improve this answer
























                          0












                          0








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





                          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 '18 at 12:28









                          Srce Cde

                          1,136511




                          1,136511























                              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





                              share|improve this answer


























                                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





                                share|improve this answer
























                                  0












                                  0








                                  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





                                  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 '18 at 12:29









                                  Harsha B

                                  2,55521733




                                  2,55521733























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





                                      share|improve this answer




























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





                                        share|improve this answer


























                                          0












                                          0








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





                                          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 '18 at 12:31

























                                          answered Nov 19 '18 at 12:23









                                          Wolfeius

                                          13




                                          13























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





                                              share|improve this answer


























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





                                                share|improve this answer
























                                                  0












                                                  0








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





                                                  share|improve this answer












                                                  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












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Nov 19 '18 at 12:33









                                                  zxy

                                                  1316




                                                  1316























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





                                                      share|improve this answer




























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





                                                        share|improve this answer


























                                                          0












                                                          0








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





                                                          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 '18 at 12:38

























                                                          answered Nov 19 '18 at 12:29









                                                          Somya Avasthi

                                                          665




                                                          665























                                                              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






                                                              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 '18 at 12:35








                                                              • 1




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
















                                                              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






                                                              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 '18 at 12:35








                                                              • 1




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














                                                              0












                                                              0








                                                              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






                                                              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 '18 at 12:54

























                                                              answered Nov 19 '18 at 12:29









                                                              Usman

                                                              744514




                                                              744514












                                                              • 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 '18 at 12:35








                                                              • 1




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


















                                                              • On point 3: for/else is a valid Python construct. The else block is executed at most once, if and only if the for loop runs to completion without ever breaking.
                                                                – timgeb
                                                                Nov 19 '18 at 12:35








                                                              • 1




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
















                                                              On point 3: for/else is a valid Python construct. The else block is executed at most once, if and only if the for loop runs to completion without ever breaking.
                                                              – timgeb
                                                              Nov 19 '18 at 12:35






                                                              On point 3: for/else is a valid Python construct. The else block is executed at most once, if and only if the for loop runs to completion without ever breaking.
                                                              – timgeb
                                                              Nov 19 '18 at 12:35






                                                              1




                                                              1




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




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











                                                              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





                                                              share|improve this answer




























                                                                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





                                                                share|improve this answer


























                                                                  0












                                                                  0








                                                                  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





                                                                  share|improve this answer














                                                                  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














                                                                  share|improve this answer



                                                                  share|improve this answer








                                                                  edited Nov 19 '18 at 13:32









                                                                  LW001

                                                                  1,50441122




                                                                  1,50441122










                                                                  answered Nov 19 '18 at 13:13









                                                                  Danish Hassan

                                                                  11




                                                                  11























                                                                      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.






                                                                      share|improve this answer























                                                                      • 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














                                                                      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























                                                                      • 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












                                                                      0








                                                                      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.






                                                                      share|improve this answer














                                                                      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














                                                                      share|improve this answer



                                                                      share|improve this answer








                                                                      edited Nov 19 '18 at 14:06









                                                                      Nils Gudat

                                                                      2,09511433




                                                                      2,09511433










                                                                      answered Nov 19 '18 at 12:19









                                                                      j.dings

                                                                      253




                                                                      253












                                                                      • I think it would be useful if you elaborated a bit. Where should this line be used? Why didn't the code in the question work?
                                                                        – KenHBS
                                                                        Nov 19 '18 at 12:51


















                                                                      • I think it would be useful if you elaborated a bit. Where should this line be used? Why didn't the code in the question work?
                                                                        – KenHBS
                                                                        Nov 19 '18 at 12:51
















                                                                      I think it would be useful if you elaborated a bit. Where should this line be used? Why didn't the code in the question work?
                                                                      – KenHBS
                                                                      Nov 19 '18 at 12:51




                                                                      I think it would be useful if you elaborated a bit. Where should this line be used? Why didn't the code in the question work?
                                                                      – KenHBS
                                                                      Nov 19 '18 at 12:51


















                                                                      draft saved

                                                                      draft discarded




















































                                                                      Thanks for contributing an answer to Stack Overflow!


                                                                      • Please be sure to answer the question. Provide details and share your research!

                                                                      But avoid



                                                                      • Asking for help, clarification, or responding to other answers.

                                                                      • Making statements based on opinion; back them up with references or personal experience.


                                                                      To learn more, see our tips on writing great answers.





                                                                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                                      Please pay close attention to the following guidance:


                                                                      • Please be sure to answer the question. Provide details and share your research!

                                                                      But avoid



                                                                      • Asking for help, clarification, or responding to other answers.

                                                                      • Making statements based on opinion; back them up with references or personal experience.


                                                                      To learn more, see our tips on writing great answers.




                                                                      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

                                                                      MongoDB - Not Authorized To Execute Command

                                                                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                                                                      How to fix TextFormField cause rebuild widget in Flutter