I am having trouble trouble figuring out this problem any help would be appreciated.

The problem wants you to change english into pirate language. Using the string split and join methods.

This is what i have so far.

import copy

def wordReplace (wordText):
    wordDict = { "hello" : "avast" , "excuse" : "arrr" , "sir,boy,man" : "matey" , "madam" : "proud beauty" ,"officer" : "foul blaggart" , "the" : " th'" , "my": "me" , "your" : "yer" , "is": "be" , "are" : "be" , "restroom" : "head" , "restaurant" : "galley" , "hotel" : "fleabag inn" , "booty" : "treasure" , "grog" : "drink" , "jack" : "flag"}
    wordFile = (wordText)
    for line in wordFile:
        wordDict  wordText.replace(wordText,wordDict[wordText])
    return wordText

Recommended Answers

All 5 Replies

Please use (code) button

import copy

def wordReplace (wordText):
    wordDict = { "hello" : "avast" , "excuse" : "arrr" , "sir,boy,man" : "matey" , "madam" : "proud beauty" ,"officer" : "foul blaggart" , "the" : " th'" , "my": "me" , "your" : "yer" , "is": "be" , "are" : "be" , "restroom" : "head" , "restaurant" : "galley" , "hotel" : "fleabag inn" , "booty" : "treasure" , "grog" : "drink" , "jack" : "flag"}
    wordFile = (wordText)
    for line in wordFile:
        wordDict  wordText.replace(wordText,wordDict[wordText])
    return wordText
import copy

def wordReplace (wordText):
    wordDict = { "hello" : "avast" , "excuse" : "arrr" , "sir,boy,man" : "matey" , "madam" : "proud beauty" ,"officer" : "foul blaggart" , "the" : " th'" , "my": "me" , "your" : "yer" , "is": "be" , "are" : "be" , "restroom" : "head" , "restaurant" : "galley" , "hotel" : "fleabag inn" , "booty" : "treasure" , "grog" : "drink" , "jack" : "flag"}
    wordFile = (wordText)
    for line in wordFile:
        wordDict  wordText.replace(wordText,wordDict[wordText])
    return wordText
import copy

def wordReplace (wordText):
    wordDict = { "hello" : "avast" , "excuse" : "arrr" , "sir,boy,man" : "matey" , "madam" : "proud beauty" ,"officer" : "foul blaggart" , "the" : " th'" , "my": "me" , "your" : "yer" , "is": "be" , "are" : "be" , "restroom" : "head" , "restaurant" : "galley" , "hotel" : "fleabag inn" , "booty" : "treasure" , "grog" : "drink" , "jack" : "flag"}
    wordFile = (wordText)
    for line in wordFile:
        wordDict  wordText.replace(wordText,wordDict[wordText])
    return wordText

What is line 5 supposed to say, now it makes wordFile to point to the same text as wordText, which does not make sense.

Maybe you should read about with statement and opening/reading files?

def wordReplace (wordText):
    # key must be for example "sir,boy,man" to get "matey" not 'sir', not 'sir,boy', not even 'sir, boy,man' (extra space)
    wordDict = { "hello" : "avast" , "excuse" : "arrr" , "sir,boy,man" : "matey" , "madam" : "proud beauty" ,"officer" : "foul blaggart" , "the" : " th'" , "my": "me" , "your" : "yer" , "is": "be" , "are" : "be" , "restroom" : "head" , "restaurant" : "galley" , "hotel" : "fleabag inn" , "booty" : "treasure" , "grog" : "drink" , "jack" : "flag"}
    wordFile = (wordText) # wordFile and wordText are same thing
    for line in wordFile: #loop over characters in string wordText
        print line
##        wordDict  wordText.replace(wordText,wordDict[wordText]) # ??? SyntaxError
    return wordText # return

I think the main problem is that you're trying to apply the string operator .replace() on a dictionary. You need to get the individual keys and do a replace for each of them:

def wordReplace (wordText):
    """ Takes a list of strings and prints out the 
    equivalent text in Pirate-speak."""
    
    wordDict = { "Hello" : "Avast", "hello" : "avast", "excuse" : "arrr" , 
    "sir" : "matey" , "man" : "matey" ,"madam" : "ye proud beauty",
    "officer" : "foul blaggart" , "the" : " th'" , "my": "me" ,
    "your" : "yer" , "is": "be" , "are" : "be" , "restroom" : "head" , 
    "restaurant" : "galley" , "hotel" : "fleabag inn" , "treasure" :"booty",
    "drink" : "grog" , "flag" : "jack"}
    
    wordFile = (wordText) # wordFile and wordText are same thing
    for line in wordFile: #loop over characters in string wordText
        for key in wordDict.keys():
            line = line.replace(key, wordDict[key])
        print(line),
    return wordText # return

textFile = file("test.txt")


text = textFile.readlines()
wordReplace(text)

Applying this to the following file:

Hello, good sir. And what would
you and madam like to spend your 
hard earned treasure on this evening?

prints out:

Avast, good matey. And what would
you and ye proud beauty like to spend yer 
hard earned booty on thbe evening?

Not perfect by any means, but it does work this way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.