Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Not Printing
#1
This is the assignment:

There are aardvarks on the loose! We need you to check whether there are any aardvarks hidden in a text file.

To start, your program should read in a file input.txt, one line at a time, numbering the lines from 1. You should check 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. So, if the file input.txt contains this:


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


Output:
Aardvark on line 3 Aardvark on line 4

If you check carefully, you should be able to see why.


If aardvark can be spelt out on each line with the letters provided in any order, the program should say so.

This is what I have done so far:

lines=[]
f = open('input.txt')
line = f.readline()
x=0
while line:
  x=x+1
  if line.count('a')==3:
    if line.count('r')==2:
      if line.count('d')==1:
        if line.count('v')==1:
          if line.count('k')==1:
            lines.append(x)
          else:
            line = f.readline()
        else:
          line = f.readline()
      else:
        line = f.readline()
    else:
      line = f.readline()
  else:
    line = f.readline()

    
for i in lines:
  print('Aardvark on line',i)
My output is blank because it is not printing anything.

I have tried moving the function of line 25-26 to line 12 but that still is not changing anything.

Please help.
Reply
#2
If you are allowed to use dictionaries, you can do it this way:
lines = {}
with open('input.txt') as f:
    for lineno, line in enumerate(f):
        idx = str(lineno + 1)
        lines[idx] = {}
        lines[idx]['line'] = line.lower()
        hits = 0
        if line.count('a') > 2:
            hits += 1
            if line.count('r') > 1:
                hits += 1
                if line.count('d') > 0:
                    hits += 1
                    if line.count('v') > 0:
                        hits += 1
                        if line.count('k') > 0:
                            hits += 1
        if hits == 5:
            lines[idx]['cando'] = True
        else:
            lines[idx]['cando'] = False

    for lineno, value in lines.items():
        if value['cando']:
            print('line {}, {}'.format(lineno, value['line']))
Reply
#3
Can you please make the code a bit more simple because I cannot understand what you have done.
Reply
#4
It's pretty much what you have done, with the addition of a counter to see how many conditions are met, and storing the results in a dictionary.

The dictionary has the format(using your test data) :
  • idx is the line number of the line from the 'input.txt' file, I added 1 so that it starts with 1 instead of zero.
    It will be used as the key, (and also line number) of the dictionary number.
  • The next item (cell) added to the dictionary is the actual sentence read from the index file. It has a sub-key with the name of 'line', with the value equal to the line of text from the file. It is converted to lower case to make counting letters easier.
  • The final item added to the dictionary is a Boolean value that is true is 'Aardvark' can be spelled. The conditions that are met to determine this are explained below.
lines = {
    '1': {
        'line': 'Sentence from file'
        'cando': Boolean value from test 
    },
    '2': {
        'line': 'Sentence from file'
        'cando': Boolean value from test 
    },
    ...
}
the way the condition is met is to check the count of a's, r's, ... same as you did, with one difference, I check for the minimum
number of a's, etc. allowing that more than the minimum is OK. As each condition is met, I add 1 to 'hits'. At the end, if all conditions are met,
hits will be equal to 5 (5 conditions), otherwise aardvark cannot be built. And as a result the 'cando' cell is set to either True or False.

The final loop, iterates through the dictionary, and only prints those items that have a True 'cando' cell.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020