The following def is my assignment .. the problem is the accumulator pattern gets me very lost. I don't know how to write it.. I have asked my instructor for help and he refers me to the sad sad book we have been given.

I DO NOT WANT THIS QUESTION ANSWERED.

This def takes a string and returns pirate talk.

Example: toPirate('hello') .. should return a different word from the dictionary..
'avast'

Otherwise:
it should just return the previous word that is not in the dictionary.

Example:
toPirate('hello dad') 'avast dad' I am just lost on this and would like a similar example of a def .. so I can do my own work on this and thus not get in trouble .. thanks!

(Also sorry if my words confuse .. I don't know all the 'right words' for things yet.

#################################################################################
Also this DEF is not yet complete .. the instructor got me this far.
#################################################################################

def toPirate(string):
   dictionary={'hello':'avast','excuse':'arrr','sir':'matey','boy':'matey',
               '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'}
#initiate accumulator
   acc=[]
#update accumulator
   words=string.split()
   for x in words:
       if x in dictionary:
           acc=acc+[dictionary[x]]
#return accumulator

#################################################################################
Hopefully someone is nice enough to teach me what others just can't :(

So you want example of accumulate pattern different from yours? You should accumulate string result and put space between words, because you want to return string. Normaly in Python we would use ' '.join(generate_the_words) but that is other, generator pattern. Looks like you could use little repetion of how functions work. Have you read through the tutorial from Python's help?

Your function now does not deal with words missing from dictionary.

Here is accumulate pattern with summing numbers (primes), see also use of functions, returning string instead of number value and alternative better ready Python way with filter:

""" Accumulate sum of primenumbers less than 100 by function and
    return verbal string answer """
def isprime(n):
    """ one not so optimal isprime """
    if n < 2:
        return False
    if n % 2 == 0:
        return n == 2
    i = 3
    
    for i in range(3,n**0.5+1,2):
        if n % i == 0:
            return False
    return True

def sum_of_primes_string(limit = 100):
    """ accumulate sum of primes to until_now until limit """
    until_now = 0
    for i in range(2,limit):
        if isprime(i):
            until_now += i
        else:
            until_now = until_now ## place holder to show the other branch
    return 'The sum of primes until %i is: %i' % (limit, until_now)

print sum_of_primes_string()
print sum_of_primes_string(200)

print 'Until 100 by filter', sum(filter(isprime,range(100)))
print 'Until 200 by filter', sum(filter(isprime,range(200)))

very neat stuff! ;)

Ok I realize the Def is unfinished .. but I have no clue how to end this to return the way I want it to .. and although I ''KNOW'' there are better ways.. my teacher wants me to do this with accumulator pattern....


So it confuses me very much because he refers me to the book instead of helping me.
The book says 'this does that' and 'that does this' .. not really much help when I need examples of how to use the strings and such in a way to do this project.

Plus I am a first year student and it troubles me why they chose to do this online.

So the Def in the first post is where I am at and I am very very stuck ... maybe some examples of similar problems would help clear things up (using different words maybe and the accumulator pattern also)

thanks for posting replies guys! :D your a big big help I relly want to learn this.

FORGOT TO MENTION:
Be very basic with me I am gentle :P ... hence don't understand complicated formulas or patterns. Loop and Acc pattern is all so far.. still don't get some of that tho ... so give examples if you can and help me understand please and thank you.

This is the question I was given.
English to Pirate Talk: Your toPirate function should take a string argument representing a complete English sentence. The function should return (not print!) the equivalent Pirate talk sentence. It should translate all the English words in the table, plus three more which you can find by searching the web for pirate talk. To keep things simple, you may omit capital letters and punctuation in both English and Piratese. Hints: Use the string split and join methods to convert a string of words into a list of words and vice-versa.

The words in the dictionary above are the words used.. I have not added the extra words from searching yet to simplify this for you to understand what to actually use.

Ok, here another example (join method I do not use)

numberdict={ 'one':'1', 'three':'3', 'seven':'7' }

def numbers(numbertext):
    result = ''
    for number in numbertext.split():
        if result:
            result += ' ' # space not after empty string
        if number in numberdict:
            result += numberdict[number]
        else:
            result += number

    return result

print numbers('one seven eight four ten three zero one')

Here same with generator and join and get method with default value number:

def numbers_join(numbertext):
    return ' '.join(numberdict.get(number,number)
                    for number in numbertext.split())

look i dont ge you. do you have to search the dictionary for the word occurance and print them out + a custom words ???

help is on the way

a DEF to translate a sentence you type into pirate language (any sentence based on the dictionary words)

dictionary={'hello':'avast','excuse':'arrr','sir':'matey','boy':'matey',
               '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'}

The DEF shown is FAR from complete it even could be wrong.

Example:

toPirate(sentence)

run module toPirate: via IDLE

toPirate(hello excuse me boy)
avast arr me matey

sentence can be anything .. it changes when split and ran through the dictionary.

What am I not saying right to confuse you? Please ask questions and I'll try to get back at you soon!


###################
i think tonyjv's example deals with this right? I am confused but if given a response to this maybe I can start fiddling with it and understand more :D ... if you see something wrong with what i need/what he posted let me know also.. have to say I looked over it and it doesn't seem right :( or maybe I don't see it


Thanks guys much much help already

numberdict={ 'one':'1', 'three':'3', 'seven':'7' }

def numbers(numbertext):
    result = ''
    for number in numbertext.split():
        if result:
            result += ' ' # space not after empty string
        if number in numberdict:
            result += numberdict[number]
        else:
            result += number

    return result

print numbers('one seven eight four ten three zero one')

Problem I see .. parameters should just be one sentence like "hello how are you"

and the def should return pirate sentence..


in this def I see

(numbertext)

would this be the sentence .. and the DEf
would use the

numberdict

(dictionary right?) to translate?

if so this may work.. and the acc is that the "result" in the DEF ..

also would this return a sentence and not a list?>

Sorry for being a problem :( I have a book and the examples suck..

dic={ 'hello':'avast','excuse':'arrr','sir':'matey','boy':'matey',
               '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' }

def toPirate(string):
    result = ''
    for word in dic.split(string):
        if result:
            result += ' '
        if word in dic:
            result += [dic[word]]
        else:
            result += word

    return result

any help .. I took a shot at it :( just stumped.

Looks good except the extra [] at line 12.

The dic Dictionary is not declared anywhere in your function toPirate.
Neither is it refferenced to. how do you code.
line #8

for words in dic.split(string) #?????

define your function to either contain the dic. Such as

def toPirate(string, **dic):

here you can pass the dic as the last arg.

Also on line 12
result+=[dic[word]] ## wrong
just code

result+=dic[word]

Hope you get the idea ;)

Could an admin delete this post instructor helped me with it and there is no real answer that i saw here anyways thank.

We dont solve problems here my good friend.

we share ideas and give direction.

You are welcome anytime ;)

No sorry I do not mean to say you didn't help me or give me ideas ..

I simply mean to say I am too new to understand them at the moment unless they are put in ''noobie'' terms :D so don't feel you didn't help cause with the last 2-3 posts you helped me alot .. I am just new to the forum and also didn't know there was a second page on this until now :D thanks guys!!

dic={ 'hello':'avast','excuse':'arrr','sir':'matey','boy':'matey',
               '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' }

def toPirate(string, dic):
    result = ''
    for word in dic.split(string):
        if result:
            result += ' '
        if word in dic:
            result += dic[word]
        else:
            result += word

    return result

figured the instructor help would have solved this problem but I ran out of time and was still lost ..

this is what I have so far and it results in the following:

>>>toPirate('hello sir would you liek to die')
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
toPirate('hello sir would you liek to die')
TypeError: toPirate() takes exactly 2 positional arguments (1 given)
>>>

This was ran in idle shell.. to test the following string .. am I entering something wrong haha I probably am typing something wrong cause I am lost ATM.

would toPirate() have to be a 'string' right? or not a string?

Sorry to triple post .. tried to edit but didn't let me :(

what is

+=

I cannot find it in my book

all i have is <, <=, >, >=, ==, and also != meanings in my python book ...

for word in dic.split(string):

means split dic where occurs string string, which is not what you mean, you want to split string from default place (no parameters), which is whitespace. So you want:

for word in string.split():

Did you prove += interactively. Use the interpreter (Python shell) to experiment:

ython 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

    ****************************************************************
    Personal firewall software may warn about the connection IDLE
    makes to its subprocess using this computer's internal loopback
    interface.  This connection is not visible on any external
    interface and no data is sent to or received from the Internet.
    ****************************************************************
    
IDLE 2.6.6      
>>> a='a'
>>> a+='b'
>>> a
'ab'
>>>

You are not calling the function with dictionary as you defined. Take out the dic parameter. Function can read it without problem as it is globally defined.

Hi, to be precise

1. Your method/function needs 2 arg but you gave only 1.
2. The logic of your function is not complete and will never get you what you want.
The logic flow is not ok.

If you want a fuction to check up words in your dic, its a piece of cake. But untils know... we are going in circles i think.

Put the cards on the table.... what do you really need. We need to solve this once and for all.
;)

def toPirate(string):
   dictionary={'hello':'avast','excuse':'arrr','sir':'matey','boy':'matey',
               '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'}
#initiate accumulator
   acc=[]
#update accumulator
   words=string.split()
   for x in words:
       if x in dictionary:
           acc=acc+[dictionary[x]]
#return accumulator
   return acc

this is the current code and working ...

it takes a string and splits it into a list

the list is then ran through the def and whatever the def is ... is returned

the def is missing key components tho .. the list needs to return words that are not in the dic just the way they were when entered .. but does not

and also the string is made into a list .. and before being returned .. needs to be converted back into a string and returned just as typed.. (other than the change of some words to pirate speak)

This def works perfect besides returning a string .. it returns a list.. and doesn't return words not found in the Dictionary ..

any help/ideas for this simple Def? thanks I am sticking with this Def from now on ... as it actually works right but needs changed to be more precise on what I need

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.