Reputation: 69
Alright so my teacher gave me the assignment below, and we have only been working with dictionaries for about a day:
1) Create a dictionary to translate a sentence from one language to another (such as Spanish to English). 2) The program should then write a sentence in the first language, then in the second language.
So far, my code is this:
def userinput():
inputsentence = input("What sentence would you like to translate?(Hint! Make the sentence I speak _ more than my friends")
spanoreng = input("Is this sentence Spanish or English?(Please enter span or eng")
return(inputsentence,spanoreng)
def spantoeng(spantoengtras,inputsentence):
inputsentence.lower()
inputsentence.split()
print(inputsentence)
def engtospan(engtospantrans,inputsentence):
inputsentence.lower()
inputsentence.split()
print(inputsentence)
def main():
spantoengtrans = {'yo' : 'I', 'hablo' : 'speak', 'espanol' : 'spanish', 'ingles' : 'english', 'mas' : 'more', 'de' : 'than','mis' : 'my', 'amigos' : 'friends'}
engtospantrans = {'I' : 'yo', 'speak' : 'hablo', 'spanish' : 'espanol', 'english' : 'ingles', 'more' : 'mas', 'than' : 'de','my' : 'mis', 'friends' : 'amigos'}
(inputsentence,spanoreng) = userinput()
if spanoreng == 'span':
spantoeng(spantoengtrans,inputsentence)
elif spanoreng == 'eng':
engtospan(engtospantrans,inputsentence)
else:
print("please type span or eng")
(inputsentence,spanoreng) = userinput()
main()
All I'm having trouble with is changing the list using the dictionary. Also, my .lower() seems to not be doing anything. help?
EDIT: realized my mistake. thanks.
Upvotes: 3
Views: 17260
Reputation: 1338
You should assign variables to the outputs of .lower()
and then apply .split()
to that variable. The output of that (saved as a variable) is what you want to return (or print in your current code).
Upvotes: 0
Reputation: 76
This doesn't answer your primary question, but the reason that your lower() calls are not working is because you are calling inputsentence.lower()
without assigning the returned value to a variable, so all it does is return the string in lowercase, but nothing is pointing to it. The original string remains the same. The same thing happens when you do inputsentence.split()
, the list is created and returned, but it goes nowhere. Ultimately when you return inputsentence
you are returning the exact same value you gave it, making lower() and split() do nothing.
If you want to replace the value of the variable inputsentence
each time you use a string method you should do
inputsentence = inputsentence.lower().split()
Upvotes: 0
Reputation: 2619
To start, you only need one translate
function. The goal of this function should be to take some sentence and some dictionary, and output a sentence in the other language. To do that, I'd do something like this:
def translate(trans,inputsentence):
inputsentence.lower()
words = inputsentence.split(' ')
new_sentence = [trans[w] for w in words]
print(' '.join(new_sentence))
Next, for you input functions, you need to be calling raw_input()
not input()
. This will clean up the syntax errors.
def userinput():
inputsentence = raw_input("What sentence would you like to translate?(Hint! ... > ")
spanoreng = raw_input("Is this sentence Spanish or English? > ")
return(inputsentence,spanoreng)
And that should pretty much do it, if you rename function calls inside main()
appropriately.
Upvotes: 0
Reputation: 881555
I don't see any list -- I only see strings, which are immutable, and no use of the dictionary.
So the core function could be:
def translate(sentence, transdict):
words = sentence.split()
trans = [transdict.get(w.lower(),w) for w in words]
print(' '.join(trans))
split
makes the sentence into a list of whitespace-separated words; then the list comprehension makes it into a list of translated words (leaving words alone if they're not in the dictionary); finally we join the latter list back into a space-separated sentence.
Of course this leaves a lot to be desired, but it's hard to do better without regular expressions -- and if you've been using dictionaries for just a day regular expressions may be well beyond your studies so far. In case they're not:
import re
def maketrans(somedict):
def trans(mo):
word = mo.group()
return somedict.get(word.lower(), word)
return trans
and then
translated = re.sub(r'\w+', maketrans(right_dict), sentence)
print (translated)
will preserve punctuation and spacing. But what between RE's and higher order functions, I suspect you'd better ignore this one until later in your Python studies:-).
Upvotes: 4