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.