Jun-08-2022, 05:18 AM
(This post was last modified: Jun-08-2022, 06:49 PM by Gribouillis.)
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):
I get:
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
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.
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.