jcthread
jcthread

Reputation: 25

Trying to write a code for translating input

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?

Upvotes: 0

Views: 399

Answers (4)

ron_olo
ron_olo

Reputation: 146

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())

Upvotes: 0

Mark Tolonen
Mark Tolonen

Reputation: 177481

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?

Upvotes: 3

Lisa
Lisa

Reputation: 204

There are two issues here.

  1. 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.

  2. 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.

Upvotes: 0

EliKor
EliKor

Reputation: 199

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(" ")

Upvotes: 0

Related Questions