Taking a class on Python at my school and we are going over branching in python and one of the practice exercises is a “tweet decoder” the original code is below. But what I need help with is with modifying the code to:
Allow the user to enter a complete tweet (160 characters or less) as a single line of text. Search the resulting string for abbreviations and print a list of each abbreviation along with its decoded meaning. and
Convert the user’s tweet to a decoded tweet, replacing the abbreviations directly within the tweet.
any help on this would be appreciated. this is not an assignment, just a practice but it’s giving me a headache.
tweet = input(‘Enter abbreviation from tweet:\n’)
if tweet == ‘LOL’:
print(‘LOL = laughing out loud’)
elif tweet == ‘BFN’:
print(‘BFN = bye for now’)
elif tweet == ‘FTW’:
print(‘FTW = for the win’)
elif tweet == ‘IRL’:
print(‘IRL = in real life’)
print(“Sorry, don’t know that one”)
Hi there and welcome to the forums!
What you would want to be looking for when searching up this stuff is looking for substrings within a string. There are a couple of different methods for doing this that would likely all be valid in this situation. You don’t need to find every instance of an abbreviation, just knowing it exists within a string would be enough.
Replacing the abbreviations could in theory be a little more complex as you would need to be able to locate every instance and replace it without replacing instances of that substring in other words. However there are existing methods you can find that can do this for you also.
I don’t want to give any away as a big part of learning to code is learning how to effectively use Google to find the code you need. However I have given a couple of keyword hints you can use to find what you are looking for. Hopefully that helps!
Thanks for the quick reply! and I agree, struggling is a good way to learn, and I appreciate the help! I was headed the dict, list path, but we did go over substrings in this lesson, so I’ll start there!
Alright, I’m back, and I got the following to work, except for the else function. This isn’t exactly a search bar type situation, so I don’t think I need an else function on this example, right?
tweet = input(‘Enter Tweet here:\n’)
if ‘LOL’ in tweet:
print(‘Laughing out loud’)
if ‘BFN’ in tweet:
print(‘bye for now’)
if ‘FTW’ in tweet:
print(‘or the win’)
if ‘IRL’ in tweet:
print(‘in real life’)
(“Sorry, I don’t know that one”)
Looks like you forgot to add the
print keyword ahead of the brackets in the
else! It’s not always necessary to have an else in this type of situation, but I like having it as it’s like a mini error-handler, and lets you know when something has broken.
Wow!! I was so focused on getting the other stuff to work, and had been working on it ALL day that I missed that! if I had any hair to pull, I would have pulled it last night lol thanks, I’ll add the print as soon as I get a chance and see if it works!!