I need to make the user ask for an English sentence and then make it produce the spanish translation of it from a given data base which is given in list form. I also want the user to keep asking for a english sentence until I type in a character indictating the program to stop. Here is what I have so far along with how I am reading the code.

import string

db = [['dog', 'perro'], ['man', 'hombre'], ['woman', 'mujer'], ['house', 'casa'], ['eat',
'comer'], ['speak', 'hablar'], ['walk', 'caminar'], ['black', 'negro'], ['white', 'blanco'],
['large', 'grande'], ['fat', 'gordo'], ['skinny', 'flaca'], ['street', 'calle'], ['from', 'de'],
['to', 'a'], ['in', 'en'], ['the', 'el']]



def toSpanish2(x):


    for i in range(len(string.split(x)):    # for all the elements in x after converting the string to lists
        for j in range(len(db)):              # for all the elements in db
              if string.split(x)[i]==db[j]:   # if any element of x is equivalent to any element in the database
                    if db[j][0]==string.split(x)[i]:  # if the 0th element = and element of i
                            return db[j][1]               # return the 1st element in the list of db
              if x == "null":                        # if x equals null   
                   Break                          #then terminate the program
        return string.join(x)    #if one of the conditions are met, return the string that was converted into list form back into regular string form
x=raw_input("Please type an English Sentence:") # Make user ask for english sentence to be converted

If I am reading this right it should look for the words in the list and print them out accordingly. Is there an error or a misinterpertation of my code.


My friend's code translates the english sentence but doesn't seem to work all to properly.

import string
x=raw_input(" Please type an English sentence: ")
db = [['dog', 'perro'], ['man', 'hombre'], ['woman', 'mujer'], ['house', 'casa'], ['eat',
'comer'], ['speak', 'hablar'], ['walk', 'caminar'], ['black', 'negro'], ['white', 'blanco'],
['large', 'grande'], ['fat', 'gordo'], ['skinny', 'flaca'], ['street', 'calle'], ['from', 'de'],
['to', 'a'], ['in', 'en'], ['the', 'el']]

def toSpanish2(x):
    sum=[]
    for i in range (len(x.split())):
       for j in range(len(db)):
            if (x.split())[i] in db[j][0]:
                db[j][0]=db[j][1]
                sum.append(db[j][0])
    return " ".join(sum)

I don't see why he uses x.split but not and why there is a need to use append.
For his code when he types out an english sentence such as

" the fat man fat"
it prints "el gordo hombre"

when it should say
"el gordo hombre gordo"


Can anyone please assist us and provide us with enlightenment.

This line
db[j][0]=db[j][1]
overlays the English word in the list with the Spanish word. And do not use "sum" as it is already taken by Python for summing numbers, and "i", "l" or "O" are not good choices for single digit variable names as they can look like numbers.

db = [['dog', 'perro'], ['man', 'hombre'], ['woman', 'mujer'], ['house', 'casa'], ['eat',
'comer'], ['speak', 'hablar'], ['walk', 'caminar'], ['black', 'negro'], ['white', 'blanco'],
['large', 'grande'], ['fat', 'gordo'], ['skinny', 'flaca'], ['street', 'calle'], ['from', 'de'],
['to', 'a'], ['in', 'en'], ['the', 'el']]
 
def toSpanish2(x):
    spanish=[]
    x_split = x.split()     ## don't do the same thing every time through the loop
    for word in x_split:
       for j in range(len(db)):
            ## in keyword will return a false positive, i.e. "the" in "they"
            if word == db[j][0]:
                spanish.append(db[j][1])
    return " ".join(spanish)

x=raw_input(" Please type an English sentence: ")
print toSpanish2(x)

and provide us with enlightenment

The wise programmer is told about Tao and follows it. The average programmer is told about Tao and searches for it. The foolish programmer is told about Tao and laughs at it.

If it were not for laughter, there would be no Tao.

The highest sounds are hardest to hear.
Going forward is a way to retreat.
Great talent shows itself late in life.
Even a perfect program still has bugs.

From http://www.canonical.org/~kragen/tao-of-programming.html#book1

A simpler way is to convert the list of [english, spanish] lists to a dictionary of english:spanish pairs:

def eng2spanish(text, data):
    '''
    takes each word from an english text and
    prints out the spanish word given in data
    if the word is not found it prints the default '---'
    '''
    data_dict = dict(data)
    for word in text.split():
        # Python3
        print(data_dict.get(word, '---'), end=' ')
        # Python2
        #print(data_dict.get(word, '---')),
    
    
db = [['dog', 'perro'], ['man', 'hombre'], ['woman', 'mujer'], ['house', 'casa'], ['eat',
'comer'], ['speak', 'hablar'], ['walk', 'caminar'], ['black', 'negro'], ['white', 'blanco'],
['large', 'grande'], ['fat', 'gordo'], ['skinny', 'flaca'], ['street', 'calle'], ['from', 'de'],
['to', 'a'], ['in', 'en'], ['the', 'el']]

# come up with an english text for testing
text1 = 'the fat man in the white house'
eng2spanish(text1, db)

print()

# test a word not in db
text2 = 'the fat man lives in the white house'
eng2spanish(text2, db)
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.