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

why is this code not printing the last word http://dpaste.com/780746/ it only prints john eats

OpenStudy (anonymous):

add a space at the last of the string, like: 'john eats bread '. It's because only when c == ' ', than the word would be printed.

OpenStudy (anonymous):

you're only printing out the complete word if a space(' ') is detected. You don't have space at the end of string. If you try with 'John eats bread ', you will get to know the problem.

OpenStudy (anonymous):

oh! I didn't realise Jack said the same thing. So stupid of me. Sorry!

OpenStudy (lopus):

this code print "john eats bread", your code is good, you can comment print word and word=' ' write in only line. >>>word=' ' >>>for c in 'john eats bread': print c if c!=' ': word = word + c else: #print word word = ' '

OpenStudy (lopus):

for c in 'john eats bread ' -->you need a space for c in 'john eats bread_'

OpenStudy (anonymous):

in the following code http://dpaste.com/781646/ the the last word which is bread is translated and yet we don't have a space at the end of the sentence

OpenStudy (anonymous):

Bread is translated because of the function call translateWord() lin line 22. It's not translated in Line 19-20. All the other words are translated when they hit a space character in line 19-20. But since bread doesn't have a space char after it, it never goes into the else construct and just goes to return. But the variable word holds "bread". The return statement calls translateWord with word=bread, concatenates and returns. That's how bread is translated.

OpenStudy (anonymous):

i hav got it thanks

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!