A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 2 years ago

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

  • This Question is Closed
  1. anonymous
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  2. anonymous
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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'

  3. anonymous
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  4. anonymous
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  5. anonymous
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  6. anonymous
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  7. anonymous
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  8. anonymous
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    The print statement iss not in a loop.

  9. anonymous
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  10. anonymous
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  11. anonymous
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  12. anonymous
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  13. anonymous
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  14. anonymous
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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?

  15. anonymous
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Thank you I will try

  16. anonymous
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  17. anonymous
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  18. anonymous
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  19. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.