0

I am working on a code for the tutorial. The code reads in a file input.txt, and checks each line to see whether the letters in the line can be used to make the word "aardvark" (Uppercase letters can be used as well).

Here is my code:

with open("input.txt", "r") as text:

  for w in text:
    a = w.count('a')
    r = w.count('r')
    d = w.count('d')
    v = w.count('v')
    k = w.count('k')

    if a >= 3 and r >= 2 and d >= 1 and v >= 1 and k >= 1:
      print("Aardvark on line", w)

Here is my file:

No aardv*rks here!
Only armadillos and anteaters.
Animals are run down: very awful road kill.
I prefer a quick guacamole made from avocados.

The code printed out:

Aardvark on line Animals are run down: very awful road kill.
Aardvark on line I prefer a quick guacamole made from avocados.

The code worked fine, but I want to print out the line number instead of the line it self. Here is how I want to print out:

Aardvark on line 3
Aardvark on line 4

How do I do it?

Not_A_Hacker
  • 101
  • 1
  • 8

1 Answers1

0

The better way is to use the built-in function enumerate()

 for idx, val in enumerate(ints):
        print(idx, val)

another solution in https://www.geeksforgeeks.org/python-accessing-index-and-value-in-list/

Ayoub Benayache
  • 1,046
  • 12
  • 28
  • if you find a dupe answer, please link and flag the question as a dupe, don't copy paste the answer over. – Paritosh Singh Aug 24 '19 at 07:24
  • sometimes you need to resume a lot of reading by just what you need to make it more simple – Ayoub Benayache Aug 24 '19 at 07:26
  • No, that actively hinders site curation. Please read [this post](https://meta.stackexchange.com/questions/10841/how-should-duplicate-questions-be-handled) . Answering dupes may seem "nice" in the short run, but is very harmful for the site as a whole in the long run. Please do not answer dupes. – Paritosh Singh Aug 24 '19 at 07:33
  • ok thanks i will consider this next time – Ayoub Benayache Aug 24 '19 at 07:41