Hi,everyone. I am having problem of the last translation sentence function in Lecture 5. When I am typing into the code, it did not translate at all. Can someone explains about the 'translate(sentence)' funciton
can you post it? please use a code pasting site: - http://dpaste.com - http://pastebin.com - https://gist.github.com/ - http://pastie.org - http://codepad.org - http://ideone.com - http://www.repl.it/ paste your code there and post the link here. select Python syntax highlighting when u paste.
I'm not familiar with python but this is what it looks like it's doing. 1. It is iterating through each character in the 'sentence'. 2. If the character is not a space, then it adds that character to the end of the word. For instance, if we take the example 'John eats bread'. Since 'J' isn't a space, the variable word becomes 'J'. Then the loop takes it to the next character, 'o'. Again, since it isn't a space, it adds it to the end of the variable word. Now word is 'Jo'. It keeps doing this until the loop finds a space. So eventually word will be 'John'. 3. Once a space character is found, it calls the translateWord function, taking the word, 'John' and translating by looking up its translation in the map called EtoF. Since John isn't in there, it just returns John. As the function runs, eventually the variable word will be 'eats'. Since eats is in the EtoF map, it'll return 'mange'. 4. Once the function runs the else part (if the character is not a space) then it sets word to an empty string. I hope that helps a bit?
but i don't understand 'word=word+c'. Does it add a space behind Character J?
It's not adding a space, because of the conditional statement "if c != ' ':" In english it says, if the character isn't a space, then do this. If the sentence is made up of a bunch of characters, the variable c is going through the sentence and pointing at each character in sequence. For instance Before anything, word = '' 1st iteration of the loop, word = 'J' 2nd iteration of the loop, word = 'Jo' 3rd iteration of the loop, word = 'Joh' 4th iteration of the loop, word = 'John' Now, next time the loop runs, c is a space, so it goes the else part of the conditional statement.
i understand that c !=' ' means that if it is not a space, but I don't understand the definition of translation=' '? Does that mean assign a space to translation? I am confused about these two coma things.
oh i see. That makes sense
so u mean at first word is nothing, and after the loop, it adds things into it
Thanks a lot! I got it
Where it says translation = '', its basically assigning a blank value to translation, there are no characters, nothing. Where it says translation = ' ', its assigning a space to the variable. Either way, if you were to print out translation (assuming you don't modify it, which is what the loop is doing) it would print out either nothing (like in the first one) or a space (like in the second).
Great! Cheers and best of luck in coding :)
Join our real-time social learning platform and learn together with your friends!