1

My current code works but not how i want it to work. Currently if i enter a word i.e. "compc" and then search for the character "c" the output will be:

'c' found at index 0
Sorry, no occurrences of 'c' found at index 1
Sorry, no occurrences of 'c' found at index 2
Sorry, no occurrences of 'c' found at index 3
'c' found at index 4

but what i want it to do is only show:

'c' found at index 0
'c' found at index 4

If no characters were found then simply:

Sorry, no occurrences of 'c' found

My current code is:

print("This program finds all indexes of a character in a string. \n")

inStr = input("Enter a string to search:\n")
searchChar = input("\nWhat character to find? ")
searchChar = searchChar[0]

anyFound = False
startAt = 0


index = startAt


while index < len(inStr):
    if inStr[index] == searchChar:
        anyFound = True


    if anyFound == True:
        print ("'" + searchChar + "' found at index", index)
        index = index + 1
        anyFound = False


    else:
        anyFound == False
        print("Sorry, no occurrences of '" + searchChar + "' found")
        index = index + 1

4 Answers 4

1
print("This program finds all indexes of a character in a string. \n")

in_str = input("Enter a string to search:\n")
search_char = input("\nWhat character to find? ")
search_char = search_char[0]

any_found = False

for index, char in enumerate(in_str):
    if char == search_char:
        print("'%s' found at index %d" % (search_char, index))
        any_found = True

if not any_found:
    print("Sorry, no occurrences of '%s' found" % (search_char))
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply, although I was trying to figure out what's wrong with my code. Although it would be more efficient in a for loop, i'm trying to accomplish the task within a while loop. Any ideas on that?
You have to print Sorry... after for, not inside for.
0

change your while loop structure:

anyFound = False #initialize as False
while index < len(inStr):
    if inStr[index] == searchChar:
        print ("'" + searchChar + "' found at index", index)
        anyFound = True #will change if any are found
    #don't do anything if the char is not the same
    index = index + 1

#following code will only run if anyFound wasn't changed
if not anyFound: 
    print("Sorry, no occurrences of '" + searchChar + "' found")

Comments

0

I'd move your anyFound boolean outside and only set it once you've found something. I kept everything else pretty similar to what you had except I brought out the index increment.

anyFound = False
while index < len(inStr): 

    if inStr[index] == searchChar:
        print ("'" + searchChar + "' found at index", index)
        anyFound = True

    index = index + 1

if not anyFound:
    print("Sorry, no occurrences of '" + searchChar + "' found")

Comments

0

There are a few problems with your code, this seems to solve them all for Python 2:

#!/usr/bin/env python
print("This program finds all indexes of a character in a string. \n")
inStr = raw_input("Enter a string to search:\n")
searchChar = raw_input("\nWhat character to find? ")
searchChar = searchChar[0]
anyFound = False
startAt = 0
index = startAt
while index < len(inStr):
    if inStr[index] == searchChar:
        anyFound = True
        print "'" + searchChar + "' found at index " + str(index)
        index = index + 1
    index += 1
if not anyFound:
    print("Sorry, no occurrences of '" + searchChar + "' found")

The improvements I've made are:

  1. use raw_input instead of input so users can type just abca instead of "abca"
  2. move the last check outside of the while

4 Comments

you dont need to increment index outside of your while loop anymore :)
@RNar true! copy pasting madness.
also, using his use of brackets for the print function as a reference, i think that OP is using python 3; where there is no raw_input function.
Makes a lot of sense, Thank you. Positioning the increments is one thing i need to work on for sure. I removed the redundant ones however in the if statements. Also @R Nar i am using python 3, but i am able to convert the differences :) /Note; raw_input just isn't a valid function for me, however i do not have to use "string" either way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.