Open study

is now brainly

With Brainly you can:

  • Get homework help from millions of students and moderators
  • Learn how to solve problems with step-by-step explanations
  • Share your knowledge and earn points by helping other students
  • Learn anywhere, anytime with the Brainly app!

A community for students.

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?

Computer Science
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Join Brainly to access

this expert answer

SIGN UP FOR FREE
http://dpaste.com/1431048/ How to make it work?
Well, you are looking at characters, but trying to test for a string.
Changing to i wont make it work

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

http://dpaste.com/1431056/
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.
I know I need to use slicing and ranges, but I dont know how to apply them in this
All stringgs are arrays. Slicing is s[start:stop] basically. But you can use - numbers to go backwards.
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?
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.
You can end up testing more than exists. Your s[i+2]=="b": can go past the end of the string.
So how can i control it?
String index is out of range :/
Yes. And how far are you going last the end in the middle of the loop?
``` 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 ```
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))
Forget it, same mistake :/
Use ``` above and below the code block. I don't know if that can work, but there is a simpler way to solve it.
Yep...this worked...But how could I do this simpler?
Look at what I asked about how much too far you are going. How far is it past the end of the string?
2
Right. So, how could you change the range so that it goes 2 less?
so i could make the end of the range the leng - 2
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
Oh... now I see :D
Thanks a lot :D Next problem set looks much harder
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.
Thanks :)
Can you help me with the start of another one?
Sure.
I will open a new question, thanks :)

Not the answer you are looking for?

Search for more explanations.

Ask your own question