Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

osanseviero Group Title

I need to create a program that counts the string "bob" inside another string, given by the user. The bobs can overlap. What is wrong with my code?

  • 9 months ago
  • 9 months ago

  • This Question is Closed
  1. osanseviero Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    http://dpaste.com/1431048/ How to make it work?

    • 9 months ago
  2. e.mccormick Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    Well, you are looking at characters, but trying to test for a string.

    • 9 months ago
  3. osanseviero Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Changing to i wont make it work

    • 9 months ago
  4. osanseviero Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    http://dpaste.com/1431056/

    • 9 months ago
  5. e.mccormick Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    string.find(s, sub[, start[, end]]) Return the lowest index in s where the substring sub is found such that sub is wholly contained in s[start:end]. Return -1 on failure. Defaults for start and end and interpretation of negative values is the same as for slices. Loop or recurse until you find all copies. As you find one, advance how far you start in the string, or pass from just beyond if recursing.

    • 9 months ago
  6. osanseviero Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    I know I need to use slicing and ranges, but I dont know how to apply them in this

    • 9 months ago
  7. e.mccormick Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    All stringgs are arrays. Slicing is s[start:stop] basically. But you can use - numbers to go backwards.

    • 9 months ago
  8. osanseviero Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    s=raw_input("Enter a word: ") Bobs= 0 for i in range(len(s)): if s[i]=="b": if s[i+1]=="o": if s[i+2]=="b": Bobs +=1 print("Number of bobs: " + str(Bobs)) Why doesnt that work?

    • 9 months ago
  9. e.mccormick Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    The end is not inclided in the slice. ``` >>> s="This is a test" >>> s[1:] 'his is a test' >>> s[:-5] 'This is a' >>> s[2:3] 'i' >>> ``` OK, let me look.

    • 9 months ago
  10. e.mccormick Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    You can end up testing more than exists. Your s[i+2]=="b": can go past the end of the string.

    • 9 months ago
  11. osanseviero Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    So how can i control it?

    • 9 months ago
  12. osanseviero Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    String index is out of range :/

    • 9 months ago
  13. e.mccormick Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    Yes. And how far are you going last the end in the middle of the loop?

    • 9 months ago
  14. e.mccormick Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    ``` for i in range(len(s)): if s[i]=="b": if s[i+1]=="o": if s[i+2]=="b": # <== How much too far? Bobs +=1 ```

    • 9 months ago
  15. osanseviero Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    can this work? (and remember me with what can I paste code, please) s=raw_input("Enter a word: ") Bobs= 0 for i in range(len(s)): if i+2<=len(s): if s[i]=="b": if s[i+1]=="o": if s[i+2]=="b": Bobs +=1 print("Number of bobs: " + str(Bobs))

    • 9 months ago
  16. osanseviero Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Forget it, same mistake :/

    • 9 months ago
  17. e.mccormick Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    Use ``` above and below the code block. I don't know if that can work, but there is a simpler way to solve it.

    • 9 months ago
  18. osanseviero Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Yep...this worked...But how could I do this simpler?

    • 9 months ago
  19. e.mccormick Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    Look at what I asked about how much too far you are going. How far is it past the end of the string?

    • 9 months ago
  20. osanseviero Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    2

    • 9 months ago
  21. e.mccormick Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    Right. So, how could you change the range so that it goes 2 less?

    • 9 months ago
  22. osanseviero Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    so i could make the end of the range the leng - 2

    • 9 months ago
  23. e.mccormick Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    Exactly. ``` #s=raw_input("Enter a word: ") s="This is a bob test for bob find bob" Bobs= 0 for i in range(len(s)-2): if s[i]=="b": if s[i+1]=="o": if s[i+2]=="b": Bobs +=1 print("Number of bobs: " + str(Bobs)) ``` Number of bobs: 3

    • 9 months ago
  24. osanseviero Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Oh... now I see :D

    • 9 months ago
  25. osanseviero Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Thanks a lot :D Next problem set looks much harder

    • 9 months ago
  26. e.mccormick Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    You can also see one other good thing there for testing. Rather than enter a string each time, I just make a test string. That makes the trying out code faster.

    • 9 months ago
  27. osanseviero Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Thanks :)

    • 9 months ago
  28. osanseviero Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Can you help me with the start of another one?

    • 9 months ago
  29. e.mccormick Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    Sure.

    • 9 months ago
  30. osanseviero Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    I will open a new question, thanks :)

    • 9 months ago
    • Attachments:

See more questions >>>

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.