Python Forum
Need help implementing an input with file operations
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help implementing an input with file operations
#1
Hello Guys,

I am stuck on an assignment. I have done most of the work but the third step is giving me problem. So the third step is suppose to find a twitter handle or (username) with @, so it can be @anyone, any@ or @any@any@. It is suppose to report how much occurrences it finds when I type in the handle for ex. @spacex.

My problem is my output is not matching with the expected output.

Here's my code (relevant parts):

def cleanedup(s):
    alphabet = "abcdefghijklmnopqrstuvwxyz@"
    cleantext = ""
    s = s.replace("?s", "")
    s = s.replace("'s", "")
    for character in s.lower():
        if character in alphabet:
            cleantext += character
        else:
            cleantext += ""
   return cleantext

dictionary = {}
with open("elon-musk.txt") as file:
    for tweet in file:
        for word in tweet.lower().split():
            word = cleanedup(word)
            if "@" in word:
                if word in dictionary.keys():
                    dictionary[word] += 1
                else:
                    dictionary[word] = 1

while True:
    username = input("Enter username: ")
    if username in dictionary:
        print("Mentioned", (dictionary[username]), "times.")
    else:
        print("Not found")
Output should be:

Quote:Enter username: @spacex
Mentioned 187 times.
Enter username: @nasa
Mentioned 41 times.

I get:

Quote:Enter username: @spacex
Mentioned 187 times.
Enter username: @nasa
Mentioned 40 times.

So my question is what is wrong with my code that its skipping over the last mention for @nasa? Also Is there any simpler way of doing this? Without s.replace()? I need to get it as simple since that is my assignment constraints.

elon-musk.txt
Gribouillis write Jun-08-2022, 06:49 PM:
I reverted the last edit in this post, please read carefully the forum's policy regarding Editing and deleting threads/posts/account, especially the paragraph about students attempting to delete their posts.
Reply


Messages In This Thread
Need help implementing an input with file operations - by Gaijin - Jun-08-2022, 05:18 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to add the user input from file into list wilson20 8 4,500 May-03-2020, 10:52 PM
Last Post: Larz60+
  Implementing spam detector code into a GUI nikolassj 2 2,305 Apr-22-2020, 07:49 PM
Last Post: nikolassj
  Order of operations for reassignment of a variable StillAnotherDave 1 1,777 Jan-21-2020, 12:20 PM
Last Post: buran
  I'm a bit confused with these boolean operations on integers goelraghavg 6 3,834 Aug-25-2018, 03:16 PM
Last Post: perfringo
  Arithmetic operations using lists yassine 2 2,507 May-02-2018, 06:20 PM
Last Post: j.crater
  Saving the results from an input in a txt. file pythonenthusiast 2 2,838 Nov-28-2017, 03:52 PM
Last Post: wavic
Question Implementing classes , RSS feed program Miraclefruit 1 3,624 Oct-16-2017, 04:11 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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