Trying to write a code for translating input - python

I'm trying to write a code to translate an inputted message, but my output comes out blank. I thought I typed it all out correctly, and far as I can tell, I did.
This is what I typed out:
#First, create a dictionary for the pirate language
piratelanguage = {
'hello':'ahoy',
'excuse me':'arrr',
'sir':'matey',
'boy':'matey',
'man':'matey',
'madam':'proud beauty',
'officer':'foul blaggart',
'the':'th',
'my':'me',
'your':'yer',
'is':'be',
'restroom':'head',
'restaurant':'galley',
'hotel':'fleabag inn',
'coins':'doubloons',
'pirate':'buccaneer',
'friend':'mate',
'you':'ye'
}
#Next, get a message from the user
message = input("Enter a message: ")
#Then translate the message into the pirate language
piratemessage = " "
for i in range(len(message)):
if message[i].upper() in piratelanguage:
piratemessage = piratemessage + piratelanguage[message[i].upper()]
piratemessage = piratemessage + " "
#Now to print out the pirate message
print(piratemessage)
And this is my output:
Enter a message: Hello, madam, would you direct me to the nearest hotel?
It outputs the input message just fine, but there's just a blank line underneath it where the translated message should be. Thoughts?

In the question code, message[i] refers to a single character, and .upper() should be .lower(). You would need to iterate over the individual words of the message, but a simple .split() won't handle punctuation properly.
Instead, use a regular expression to match phrases using word breaks and iterate over the replacements. Then it can handle punctuation:
import re
piratelanguage = {
'hello':'ahoy',
'excuse me':'arrr',
'sir':'matey',
'boy':'matey',
'man':'matey',
'madam':'proud beauty',
'officer':'foul blaggart',
'the':'th',
'my':'me',
'your':'yer',
'is':'be',
'restroom':'head',
'restaurant':'galley',
'hotel':'fleabag inn',
'coins':'doubloons',
'pirate':'buccaneer',
'friend':'mate',
'you':'ye'
}
message = input("Enter a message: ").lower()
for key,value in piratelanguage.items():
message = re.sub(r'\b' + key + r'\b',value,message)
print(message)
Gives:
Enter a message: Hello, madam, would you direct me to the nearest hotel?
ahoy, proud beauty, would ye direct me to th nearest fleabag inn?

Your input comes in as a string so you need to split it on spaces to get the individual words, e.g.
message = input(...).split(" ")

There are two issues here.
All the keys in your pirate language dict are lowercase, but you call upper() on each word when you compare. You need to use lower() instead, so the case matches.
You are splitting up the input by character, so if the input were to be excuse me, sir, you would want to check if excuse me and sir are in the dictionary, but this code would check if e,x,c,...,r were in the dictionary. The solution here is not so simple as splitting on the spaces in the input, since there could be punctuation and excuse me has a space in it.

Try this.
piratelanguage = {
'hello':'ahoy',
'excuse me':'arrr',
'sir':'matey',
'boy':'matey',
'man':'matey',
'madam':'proud beauty',
'officer':'foul blaggart',
'the':'th',
'my':'me',
'your':'yer',
'is':'be',
'restroom':'head',
'restaurant':'galley',
'hotel':'fleabag inn',
'coins':'doubloons',
'pirate':'buccaneer',
'friend':'mate',
'you':'ye'
}
message = input("Enter a message: ")
piratemessage = ""
for i in message.split(): #split message to iterate on each word
new_word = i.lower()
temp_word = ('').join([s for s in new_word if s.isalnum()])
if temp_word in piratelanguage.keys(): # if word is in pirate language
new_word = new_word.replace(temp_word, piratelanguage[temp_word])
piratemessage += new_word + ' '
print(piratemessage.capitalize())

Related

how to remove instances and possible multiple instances of a certain word in a string and return a string (CODEWARS dubstep)

I have had a go at the CODEWARS dubstep challenge using python.
My code is below, it works and I pass the kata test. However, it took me a long time and I ended up using a brute force approach (newbie).
(basically replacing and striping the string until it worked)
Any ideas with comments on how my code could be improved please?
TASK SUMMARY:
Let's assume that a song consists of some number of words (that don't contain WUB). To make the dubstep remix of this song, Polycarpus inserts a certain number of words "WUB" before the first word of the song (the number may be zero), after the last word (the number may be zero), and between words (at least one between any pair of neighbouring words), and then the boy glues together all the words, including "WUB", in one string and plays the song at the club.
For example, a song with words "I AM X" can transform into a dubstep remix as "WUBWUBIWUBAMWUBWUBX" and cannot transform into "WUBWUBIAMWUBX".
song_decoder("WUBWEWUBAREWUBWUBTHEWUBCHAMPIONSWUBMYWUBFRIENDWUB")
# => WE ARE THE CHAMPIONS MY FRIEND
song_decoder("AWUBBWUBC"), "A B C","WUB should be replaced by 1 space"
song_decoder("AWUBWUBWUBBWUBWUBWUBC"), "A B C","multiples WUB should be replaced by only 1 space"
song_decoder("WUBAWUBBWUBCWUB"), "A B C","heading or trailing spaces should be removed"
Thanks in advance, (I am new to stackoverflow also)
MY CODE:
def song_decoder(song):
new_song = song.replace("WUB", " ")
new_song2 = new_song.strip()
new_song3 = new_song2.replace(" ", " ")
new_song4 = new_song3.replace(" ", " ")
return(new_song4)
I don't know if it can improve it but I would use split and join
text = 'WUBWEWUBAREWUBWUBTHEWUBCHAMPIONSWUBMYWUBFRIENDWUB'
text = text.replace("WUB", " ")
print(text)
words = text.split()
print(words)
text = " ".join(words)
print(text)
Result
WE ARE THE CHAMPIONS MY FRIEND
['WE', 'ARE', 'THE', 'CHAMPIONS', 'MY', 'FRIEND']
WE ARE THE CHAMPIONS MY FRIEND
EDIT:
Dittle different version. I split usinsg WUB but then it creates empty elements between two WUB and it needs to remove them
text = 'WUBWEWUBAREWUBWUBTHEWUBCHAMPIONSWUBMYWUBFRIENDWUB'
words = text.split("WUB")
print(words)
words = [x for x in words if x] # remove empty elements
#words = list(filter(None, words)) # remove empty elements
print(words)
text = " ".join(words)
print(text)

Python - If a "good" word and a "bad" word contains in a letter, print it out

I have a Bad word of list and a Good word to list. My idea was to make a first search for Good words and see which meaning in a letter contains those good word but if that meaning also contains a bad word - then it shouldn't print out the meaning.
etc:
Good word: "Lo", "Yes"
Bad word: "Hate", "Not"
text_roman: "Hello guys. My name is Lo and I hate to code :')"
Meaning: "Hello guys. My name is Lo and I hate to code :')" <-- "Just a joke!!
So meaning if it search for that meaning it should tell us there is a meaning that contains good and then check if it contains Bad word. If it does then we don't want to print out the meaning but if it doesn't contain any bad word then we should print it out.
How I tried to code was:
text_roman = "Hello guys. My name is Lo and I hate to code :')"
good_word = ["Lo", "Yes"]
bad_word = ["Hate", "Not"]
for text in good_word:
if text in text_roman:
print("Yay found word " + text_roman)
for bad_text in bad_word:
if bad_text not in text_roman:
print("Yay no bad words!")
When I tried this, the output gave all the words that contains the bad words aswell unfortunately
I would loop over the bad words first, and skip them. Then, if not skipped, check for a good word
good_word = ["Lo", "Yes"]
bad_word = ["Hate", "Not"]
has_bad = False
for b in bad_word:
if b in text_roman:
has_bad = True
continue
for g in good_word:
if g in text_roman:
print("Yay found word " + g + " in text_roman")
if not has_bad:
print("Yay no bad words!")
Note: in is case sensitive, so "Hate" in "I hate case-sensitivity" will be False

How to use dictionary to verify against user input?

So i wanted to make a little bot that could hold a small conversation with a user. The only problem is that when i type one of the words in the list(being hello or hi) then i get the welcome user message, but if i type something like hello computer it gives me the TESTPHRASE message. Is there something i can put in so that it looks in the sentence of the user input and finds a word in the used list so that it can say the appropriate response.
user_greetings = {"hello", "hi"}
user_input = input("-")
if user_input in user_greetings:
print("Welcome User")
else:
print("TESTPHRASE")
When you apply in to a string and a dictionary, it will test if the entire string is a key. It looks like you want to check for either the first word in the sentence or any word in the sentence being in the dictionary.
In either case, you'd want to split the input on spaces:
words = input('-').split()
If you want to check the first word, proceed almost as before:
if words[0] in user_greetings:
print("Welcome User")
else:
print("TESTPHRASE")
If any of the words should trigger the welcome message, use any and a generator expression:
if any(x in user_greetings for x in words):
print("Welcome User")
else:
print("TESTPHRASE")
I'm getting a syntax error for your code. Try moving else to it's own line. Otherwise, your code works for me.
EDIT:
Reread the question. Your code is checking if "hello computer" is in greetings, which is {'hello', 'hi'}. "hello computer" is not in greetings. You could reverse the search and do
for greeting in user_greetings:
if greeting in user_input:
# print greeting
Otherwise, you need to add "hello computer" to your list of greetings.
Something like this would do it:
greetings = ['hello', 'hi']
input = 'hello computer'.split()
if set(greetings).intersection(set(input)):
print('Welcome')
#Mad Physicist gives a very comprehensive answer for this and I upvoted his answer.
There is another way of doing it if any word will trigger the welcome message, no matter it is capitalized or not.
user_greetings = {"hello", "hi"}
user_input = input("-").split()
# set process control.
greeting = None
for word in user_input:
if word.lower() in user_greetings:
print("Welcome User")
else:
greeting = True
if greeting:
print("TESTPHRASE")

Python GCSE - Using Lists

In the next few weeks, I will be doing a computer science GCSE. I really need this GCSE as I aspire to because an Application Developer, so any help would be amazing! For the coding part of my GCSE, I need to create a python script that allows the user to type a string and then search for a word within that string. From there, the script would have to return all of the positions of that searched word. I have some example code here:
userinp = input("Please type a sentence: ")
list1[]
string1 = userinp.split()
for x in string1:
list1.insert(x,string1[x])
search = input("Search for a word: ")
for x in range(len(string1)):
if search in list1:
print("Word found at index:",x)
Please bare in mind this code may not work 100% as this was typed on a phone.
The actual task is to recall all of the positions of the words but after countless tries of trying, I cannot make it print the other indexes of the same word. For example, if the string was "Hello my name is Jack and my favourite food is apples" and I searched 'my', the program would have to return that the word was found at indexes 2 and 7.
As this was typed on a phone, the actual question may not have come out clear, if that is the case, please just comment saying so. Otherwise, any help would be amazing!
Try this,
userinp = raw_input("Please type a sentence: ")
words = userinp.split(' ')
search = raw_input("Search for a word: ")
for idx, word in enumerate(words):
if search == word:
print("Word found at index:", idx)
Hope it helps.

Validating user input in a "fill in the blanks" program using Python

As a newbie to programming I currently have an exam where I am supposed to submit Python code for a madlibs game. What I am currently missing in my code is the validation of the user input against the word that should appear in the text.
Right now the program is able to take user input and display the full text but I don't know where to put the validation of the user input in my code, especially because I am going through a list of different words. I know that I have to define the variable somewhere to make a check against the user input but I am completely stuck right now.
This is the current working version of my code, i.e. without the attempt of the input validation against the words.
print "Welcome to my first quiz with user input"
print "Lets get cracking straight away"
print
import time
while True:
difficulty_level = raw_input("Which difficulty level would you like? Type EASY, MEDIUM or HARD to continue? ")
if difficulty_level.upper() == "EASY":
time.sleep(1)
print "Here it goes..."
print
time.sleep(1)
print "Here's your text. Should be an easy one. Just fill in the blanks for _Word_ 1-3."
print
print
print "Python is a _Word1_ language that provides constructs intended to enable clear programs on both small and large scale. Python implementation was started in December _Word2_ by Guido von Rossum. The most simple _Word3_ in Python is _Word4_ and normally used at the beginning to tell Python to write 'Hello World' on the screen."
print
# A list of replacement words to be passed in to the play game function.
parts_of_speech1 = ["_Word1_", "_Word2_", "_Word3_", "_Word4_"]
# The following is the text for the easy text..
easy_text = "Python is a _Word1_ language that provides constructs intended to enable clear programs on both small and large scale. Python implementation was started in December _Word2_ by Guido von Rossum. The most simple _Word3_ in Python is _Word4_ and normally used at the beginning to tell Python to write 'Hello World' on the screen."
# Checks if a word in parts_of_speech is a substring of the word passed in.
def word_in_pos_easy(word, parts_of_speech1):
for pos in parts_of_speech1:
if pos in word:
return pos
return None
# Plays a full game of mad_libs. A player is prompted to replace words in the easy text,
# which appear in parts_of_speech with their own words.
def easy_game(easy_text, parts_of_speech1):
replaced = []
easy_text = easy_text.split()
for word in easy_text:
replacement = word_in_pos_easy(word, parts_of_speech1)
if replacement != None:
user_input = raw_input("Type in: " + replacement + " ")
word = word.replace(replacement, user_input)
replaced.append(word)
else:
replaced.append(word)
replaced = " ".join(replaced)
print
time.sleep(1)
print "Ok, lets see your results. Does it make sense?"
print
time.sleep(1)
return replaced
print
time.sleep(1)
print easy_game(easy_text, parts_of_speech1)
# Difficulty Level Medium
if difficulty_level.upper() == "MEDIUM":
time.sleep(1)
print "Good choice. Lets see how much you know about Python"
print
time.sleep(1)
print "Here's your text. It's a tricky one that requires some more knowledge about Python. Just fill in the blanks for _Word_ 1-3."
print
print
print "A string object is _Word1_, i.e. it cannot be modified after it has been created. An important concept in Python and other programming languages is _Word2_. You use them to store a value. To assign a value to a Variable you use the _Word3_ operator. A more versatile data type in Python is _Word4_. They contain items separated by commas and within square brackets. To some extent they are similar to arrays in C."
print
# A list of replacement words to be passed in to the play game function.
parts_of_speech2 = ["_Word1_", "_Word2_", "_Word3_", "_Word4_"]
# The following are some test strings to pass in to the play_game function.
medium_text = "A string object is _Word1_, i.e. it cannot be modified after it has been created. An important concept in Python and other programming languages is _Word2_. You use them to store a value. To assign a value to a Variable you use the _Word3_ operator. A more versatile data type in Python is _Word4_. They contain items separated by commas and within square brackets. To some extent they are similar to arrays in C."
# Checks if a word in parts_of_speech is a substring of the word passed in.
def word_in_pos_medium(word, parts_of_speech2):
for pos in parts_of_speech2:
if pos in word:
return pos
return None
# Plays a full game of mad_libs. A player is prompted to replace words in ml_string,
# which appear in parts_of_speech with their own words.
def medium_game(medium_text, parts_of_speech2):
replaced = []
medium_text = medium_text.split()
for word in medium_text:
replacement = word_in_pos_medium(word, parts_of_speech2)
if replacement != None:
user_input = raw_input("Type in: " + replacement + " ")
word = word.replace(replacement, user_input)
replaced.append(word)
else:
replaced.append(word)
replaced = " ".join(replaced)
print
time.sleep(1)
print "OK, lets see your results. Does it make sense?"
print
time.sleep(1)
return replaced
print
time.sleep(1)
print medium_game(medium_text, parts_of_speech2)
# Difficulty Level Hard
if difficulty_level.upper() == "HARD":
time.sleep(1)
print "Bold move! Here we go. Check out this text. It's a tough one"
print
time.sleep(1)
print "Here's your text. This one requires quite some Python knowledge"
print
print
print "Similar to other programming languages, Python has flow controls. The most known statement is the _Word1_ statement. It can be combined with an else statement and helps to process a logic based on a specific condition. For more repetitive processing one needs to use loops. _Word2_ loops execute a sequence of statements multiple times and abbreviates the code that manages the loop variable._Word3_ loops repeat a statement or group of statements while a given condition is TRUE. It tests the condition before executing the loop body."
print
# A list of replacement words to be passed in to the play game function.
parts_of_speech3 = ["_Word1_", "_Word2_", "_Word3_", "_Word4_"]
# The following are some test strings to pass in to the play_game function.
hard_text = "Similar to other programming languages, Python has flow controls. The most known statement is the _Word1_ statement. It can be combined with an else statement and helps to process a logic based on a specific condition. For more repetitive processing one needs to use loops. _Word2_ loops execute a sequence of statements multiple times and abbreviates the code that manages the loop variable. _Word3_ loops repeat a statement or group of statements while a given condition is TRUE. It tests the _Word4_ before executing the loop body."
# Checks if a word in parts_of_speech is a substring of the word passed in.
def word_in_pos_hard(word, parts_of_speech3):
for pos in parts_of_speech3:
if pos in word:
return pos
return None
# Plays a full game of mad_libs. A player is prompted to replace words in the hard text,
# which appear in parts_of_speech with their own words.
def hard_game(hard_text, parts_of_speech3):
replaced = []
hard_text = hard_text.split()
for word in hard_text:
replacement = word_in_pos_hard(word, parts_of_speech3)
if replacement != None:
user_input = raw_input("Type in: " + replacement + " ")
word = word.replace(replacement, user_input)
replaced.append(word)
else:
replaced.append(word)
replaced = " ".join(replaced)
print
time.sleep(1)
print "OK, lets see your results. Does it make sense?"
print
time.sleep(1)
return replaced
print
print hard_game(hard_text, parts_of_speech3)
else:
print "Sorry, that was not a correct input. Please enter EASY, MEDIUM or HARD to set the difficulty level."
Here's how I would do it:
from random import shuffle
import sys
from time import sleep
# version compatibility shim
if sys.hexversion <= 0x3000000: # Python 2.x
inp = raw_input
else: # Python 3.x
inp = input
def get_word(prompt):
return inp(prompt).strip().lower()
def shuffled(words):
words_copy = list(words)
shuffle(words_copy)
return words_copy
class MadLib:
BLANK = "___"
DELAY = 1
def __init__(self, intros, prompt, words):
self.intros = intros
self.prompt = prompt
self.words = words
self.num_words = len(words)
assert prompt.count(self.BLANK) == self.num_words, "Number of blanks must match number of words!"
def play(self):
# show introduction
for s in self.intros:
sleep(self.DELAY)
print(s, flush=True)
# display madlib with blanks
sleep(self.DELAY)
print(self.prompt)
# get words from user
print("Words available: " + ", ".join(shuffled(self.words)))
gotten = [
get_word("Word {}: ".format(i))
for i in range(1, self.num_words + 1)
]
# evaluate results
num_right = sum(g == w for g,w in zip(gotten, self.words))
if num_right == self.num_words:
print("You did it!")
return True
else:
print("You got {} out of {} words right.".format(num_right, self.num_words))
return False
madlibs = {
"easy":
MadLib(
[
"Here we go...",
"Here's your text. Should be an easy one.",
"Just fill in the blanks!"
],
(
"Python is a ___ language that provides constructs intended to "
"enable clear programs on both small and large scale. "
"Python implementation was started in December ___ by "
"Guido von Rossum. The most basic ___ in Python is ___, "
"often used by beginners to tell Python to display 'Hello "
"World' on the screen."
),
['programming', '1989', 'command', 'print']
),
"medium":
MadLib(
[
"Good choice. Lets see how much you know about Python",
"Here's your text. It's a tricky one that requires some more knowledge about Python.",
"Just fill in the blanks!"
],
(
"A string object is ___, i.e. it cannot be modified after it "
"has been created. An important concept in Python and other "
"programming languages is the ___, used to store a value. "
"To assign a value to you use the ___ operator. A more "
"versatile data type in Python is ___, containing items "
"separated by commas and within square brackets and to "
"some extent they are similar to arrays in C."
),
['immutable', 'variable', 'equals', 'list']
),
"hard":
MadLib(
[
"Bold move! Here we go. Check out this text. It's a tough one!",
"Here's your text. This one requires quite some Python knowledge.",
"Just fill in the blanks!",
"Here's your text:"
],
(
"Similar to other programming languages, Python has flow "
"control commands. The most common is ___ which lets you branch "
"based on a condition. Looping commands like ___ execute "
"statements a given number of times, or ___ repeats statements "
"so long as a condition is True."
),
['if', 'for', 'while']
)
}
def main():
print(
"Welcome to my first quiz with user input!\n"
"Lets get cracking straight away."
)
while True:
choice = get_word("Which difficulty level would you like? [easy, medium, hard] (or just hit Enter to exit) ")
if not choice:
break
elif choice in madlibs:
madlibs[choice].play()
else:
print("Sorry, that was not a recognized option.")
if __name__=="__main__":
main()

Categories