Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 8 Online
OpenStudy (anonymous):

lecture 4 i cant fingure how the example for translate sentence works .where does traslate[1:] in the return function comes from

OpenStudy (anonymous):

Can you post the code? I'm having trouble finding what you're talking about.

OpenStudy (anonymous):

the last piece of lecture 4 handout

OpenStudy (anonymous):

Ah...that's Lecture 5, not Lecture 4. The best way to figure out what code does is to change it and see what happens. You'll find that if you change the code to "translate" instead of "translate[1:]" you get the exact same thing, but with a leading space. The reason for that space is the way in which translate() works. It goes through the characters in the sentence and collects them into words by looking for a space. Once it finds a space, it takes whatever is in the word variable, and appends its French translation to the translation variable. But notice that it starts by putting a space in front of the new addition to "translation"...even if nothing has been added to translation yet! That means the first time something is added, there's going to be a space in front that you don't really want. The way the program solves this problem is by returning not *all* of translation, but just everything but the first character. Does that make sense?

OpenStudy (anonymous):

yep it make sense thanks alot

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!