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

Hello guys,I'm working in problem set 4,everytime I rum this function I find 0. def find_best_shift(wordlist, text): max_num_real_words = 0 best_shift = 0 for shift in range(27): shifted_text = apply_shift(text, shift) potential_words = shifted_text.split() num_real_words = 0 for word in potential_words: if is_word(wordlist, word): num_real_words += 1 if num_real_words > max_num_real_words: max_num_real_words = num_real_words best_shift = shift return best_shift Help

OpenStudy (anonymous):

looks reasonable, maybe try some print statements like this http://dpaste.com/1383878/

OpenStudy (anonymous):

Hi , it still don't work. Traceback (most recent call last): File "<pyshell#125>", line 1, in <module> find_best_shift(wordlist, "oryh") File "/Users/Avatar/Documents/problem 4", line 135, in find_best_shift print 'shift: [] words: []'.format(shift, potential_words) AttributeError: 'str' object has no attribute 'format'

OpenStudy (anonymous):

how 'bout this http://dpaste.com/1384625/

OpenStudy (anonymous):

s = apply_shift("love", 3) >>> s ' ORYH' >>> find_best_shift(wordlist, s) shift: 0 words: ['ORYH'] 0 Samething than before!

OpenStudy (anonymous):

what could be wrong? what did the print statement print? did it print what you expected it to print? did it print what it should have printed? how many times did it print? how many times should it have printed? what other steps have you taken to find the problem and what were the results? print statements can tell you a lot about how your code is executing

OpenStudy (anonymous):

I reallt don't know what could be wrong with my function.The "print statement " print teh shift wich were 0 and the ecoded letter. It should print a number != to 0,it should print it one time and it printed exactly one time.

OpenStudy (anonymous):

is the print statement in a loop? how many times should the loop execute?

OpenStudy (anonymous):

The print statement iss not in a loop.

OpenStudy (anonymous):

did you write the function? the print statement is in a loop. ``` >>> >>> for shift in range(27): print shift 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 >>> ``` http://docs.python.org/2.7/tutorial/controlflow.html#for-statements

OpenStudy (anonymous):

I wrote a print statement. def find_best_shift(wordlist, text): max_num_real_words = 0 best_shift = 0 for shift in range(27): shifted_text = apply_shift(text, shift) potential_words = shifted_text.split() print 'shift:', shift, ' words:', potential_words num_real_words = 0 for word in potential_words: if is_word(wordlist, word): num_real_words += 1 if num_real_words > max_num_real_words: max_num_real_words = num_real_words best_shift = shift return best_shift

OpenStudy (anonymous):

ok ... when you call the function, how many times does it print? how many times should it print?

OpenStudy (anonymous):

I'd suggest to use print stmt: print shifted_text and see the output of apply_shift proc

OpenStudy (anonymous):

I try all it still don't work. So do you have an idea about how can I import feedparser?

OpenStudy (anonymous):

different topic, start a new thread. if you want your best_shift function to work you need to examine the position of your return statement. did you write the function you posted?

OpenStudy (anonymous):

Thank you I will try

OpenStudy (anonymous):

bwCA - exactly, problem with 'return' :) silly :)

OpenStudy (anonymous):

I change my return statement place it still doesn't work

OpenStudy (anonymous):

well, do not change the place :) are you certain it is at the very end of your def?

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!