Xrin
Xrin

Reputation: 87

Searching for a input character in an input string P2 in Python

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

Upvotes: 1

Views: 154

Answers (4)

John Doe
John Doe

Reputation: 57

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

Upvotes: 0

Avantol13
Avantol13

Reputation: 1059

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")

Upvotes: 0

R Nar
R Nar

Reputation: 5515

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")

Upvotes: 0

furas
furas

Reputation: 142985

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))

Upvotes: 1

Related Questions