Missing small piece to my code

Good evening, I am new to PYTHON and working through a Summer course.

Unfortunately the Professor provides less than help feedback, and I constantly edit the script while searching online for advice.

Furthermore the Test have constraints which NULL more efficient and useful PYTHON functions better suited for completing the objectives.

Below is my current code…

def cleanedup(s):
alphabet=‘@_abcdefghijklmnopqrstuvwxyz0123456789
cleantext= ‘’
for character in s.lower():
if character in alphabet:
cleantext+= character
else:
cleantext+=’ ’
return cleantext

import os
username = {}
def findMentions(filename):
with open(filename, encoding=‘utf-8’ ) as file:
for line in file:
words = cleanedup(line).split()
for word in words:
if word[0:1] == ‘@’:
if word in username:
username[word] += 1
else:
username[word] = 1
mentions =
for word in username:
mentions.append([username[word], word])

mentions.sort()
    print(mentions[-3:])

##for filename in os.listdir(‘.’):

if filename[-7:] == ‘.tweets’:

print (filename)

For the findMentions function I need to write it to PRINT the top 3 mentions in the list. Not looking for anyone to do this for me, but can somebody point me in the correct direction?

Good evening, I am new to PYTHON and working through a Summer course.

Welcome.

Furthermore the Test have constraints which NULL more efficient and
useful PYTHON functions better suited for completing the objectives.

I don’t understand this sentence.

Below is my current code…

I’m just going to recite you code here below, because someone is bound
to complain that it isn’t formatted. Indentation is vital in Python and
you’ve lost that. To insert code or other text output or error messages
you want to encose the text in “code fences”, likes of 3 backticks eg:

 ```
 your text
 goes here
 ```

There’s a </> button in the compose bar to make such a section for
you.

I’ll recite the code below:

 def cleanedup(s):
     alphabet='@_abcdefghijklmnopqrstuvwxyz0123456789'
     cleantext= ''
     for character in s.lower():
            if character in alphabet:
                  cleantext+= character
            else:
                  cleantext+=' '
     return cleantext

 import os
 username = {}
 def findMentions(filename):
     with open(filename, encoding='utf-8' ) as file:
         for line in file:
             words = cleanedup(line).split()
             for word in words:
                 if word[0:1] == '@':
                     if word in username:
                         username[word] += 1
                     else:
                         username[word] = 1
     mentions = []
     for word in username:
         mentions.append([username[word], word])

     mentions.sort()
         print(mentions[-3:])

 ##for filename in os.listdir('.'):
 ##    if filename[-7:] == '.tweets':
 ##        print (filename)

For the findMentions function I need to write it to PRINT the top 3
mentions in the list. Not looking for anyone to do this for me, but
can somebody point me in the correct direction?

Well, it looks like you’ve technically don that with the
print(mentions[-3:]) above. Does it not work?

If you want each one on its own line not looking list a list you could
write a for-loop which loops some variable name eg mention over
mentions[-3:] and calls something like print(mention).

There’s a few small other things to remark about with your code, but we
can chase such things later.

This code is surprisingly good for someone totally new to Python. Have
you used other programming languages before?

1 Like

Good evening Cameron,

I appreciate your help on 1st, proper formatting for the site, and 2nd the feedback on my code.

I’ve used SQL, a fair amount of (albeit not a programming language) MS excel, and Retail Query reports, so I feel like those 3 helped in formatting my Python code.

For the part about the Null, I was trying to say there’s restrictions on each test, that make some of Python’s functions off limits.

The Print statement in the def(findMentions) function does not work right now.
I tried closing the function with a RETURN(mentions[-3:]) and printing the Top 3 in the os.listdir(‘.’) call.

Going that route does not meet the Test requirements so I do need to get the findMentions function to print the Top3.

The findMentions function should print the data out like this,

[[15, '@alice'], [20, '@bob'], [7, '@carol'], ... ]

And the final outcome with the os.listdir(‘.’) call should produce this

nytimes.tweets
     @caityweaver 3
     @nytmag 5
     @nytparenting 5

justinbieber.tweets
     @applemusic 15
     @theellenshow 15
     @skrillex 20

I’m reviewing the Course content again, and trying to get a For loop into the defMentions.

I appreciate you responding again.

def cleanedup(s):
   alphabet='@_abcdefghijklmnopqrstuvwxyz0123456789'
   cleantext= ''
   for character in s.lower():
          if character in alphabet:
                cleantext+= character
          else:
                cleantext+=' '
   return cleantext

import os

def findMentions(filename):
   with open(filename, encoding='utf-8' ) as file:
       print(filename)
       mentions_count = {}
       for line in file:
           words = cleanedup(line).split()
           for word in words:
               if word[0:1] == '@':
                   if word in mentions_count:
                       mentions_count[word] += 1
                   else:
                       mentions_count[word] = 1

   mentions = []
   for word in mentions_count:
           mentions.append([mentions_count[word], word])

   mentions.sort()
   x = mentions[-3:]
   for mentions in x:
       print(mentions)
          

for filename in os.listdir('.'):
   if filename[-7:] == '.tweets':
       (findMentions(filename))

Just got it! The For loop is what was missing, thank you again!

1 Like

You want could do with renaming the loop variable to mention instead of mentions for clarity tho.