A community for students.
Here's the question you clicked on:
 0 viewing
osanseviero
 3 years ago
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?
osanseviero
 3 years ago
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?

This Question is Closed

osanseviero
 3 years ago
Best ResponseYou've already chosen the best response.0http://dpaste.com/1431048/ How to make it work?

e.mccormick
 3 years ago
Best ResponseYou've already chosen the best response.1Well, you are looking at characters, but trying to test for a string.

osanseviero
 3 years ago
Best ResponseYou've already chosen the best response.0Changing to i wont make it work

e.mccormick
 3 years ago
Best ResponseYou've already chosen the best response.1string.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.

osanseviero
 3 years ago
Best ResponseYou've already chosen the best response.0I know I need to use slicing and ranges, but I dont know how to apply them in this

e.mccormick
 3 years ago
Best ResponseYou've already chosen the best response.1All stringgs are arrays. Slicing is s[start:stop] basically. But you can use  numbers to go backwards.

osanseviero
 3 years ago
Best ResponseYou've already chosen the best response.0s=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?

e.mccormick
 3 years ago
Best ResponseYou've already chosen the best response.1The 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.

e.mccormick
 3 years ago
Best ResponseYou've already chosen the best response.1You can end up testing more than exists. Your s[i+2]=="b": can go past the end of the string.

osanseviero
 3 years ago
Best ResponseYou've already chosen the best response.0So how can i control it?

osanseviero
 3 years ago
Best ResponseYou've already chosen the best response.0String index is out of range :/

e.mccormick
 3 years ago
Best ResponseYou've already chosen the best response.1Yes. And how far are you going last the end in the middle of the loop?

e.mccormick
 3 years ago
Best ResponseYou've already chosen the best response.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 ```

osanseviero
 3 years ago
Best ResponseYou've already chosen the best response.0can 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))

osanseviero
 3 years ago
Best ResponseYou've already chosen the best response.0Forget it, same mistake :/

e.mccormick
 3 years ago
Best ResponseYou've already chosen the best response.1Use ``` above and below the code block. I don't know if that can work, but there is a simpler way to solve it.

osanseviero
 3 years ago
Best ResponseYou've already chosen the best response.0Yep...this worked...But how could I do this simpler?

e.mccormick
 3 years ago
Best ResponseYou've already chosen the best response.1Look at what I asked about how much too far you are going. How far is it past the end of the string?

e.mccormick
 3 years ago
Best ResponseYou've already chosen the best response.1Right. So, how could you change the range so that it goes 2 less?

osanseviero
 3 years ago
Best ResponseYou've already chosen the best response.0so i could make the end of the range the leng  2

e.mccormick
 3 years ago
Best ResponseYou've already chosen the best response.1Exactly. ``` #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

osanseviero
 3 years ago
Best ResponseYou've already chosen the best response.0Thanks a lot :D Next problem set looks much harder

e.mccormick
 3 years ago
Best ResponseYou've already chosen the best response.1You 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.

osanseviero
 3 years ago
Best ResponseYou've already chosen the best response.0Can you help me with the start of another one?

osanseviero
 3 years ago
Best ResponseYou've already chosen the best response.0I will open a new question, thanks :)
Ask your own question
Sign UpFind more explanations on OpenStudy
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
 Engagement 19 Mad Hatter
 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.