Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys # so that I can use stdout
- charList = [] # list to store all of the split up characters
- searchChar = "n" # set what character we are searching for
- index = 0 # counter for the loop
- indexList = [] # list to store the indexes at which the program finds searchChar
- charList = list(input("enter your string here!\n")) # get the string from the use
- # then put each letter of the string in charList
- while index < len(charList): # while the value of index is less than the number of indexes in charList
- try:
- if charList[index] == searchChar:
- print(searchChar, " found at index: ", index) # if seach char is found,
- # print the index at which it was found
- indexList.append(index) # append this index to indexList
- else:
- print(searchChar, " not found at index: ", index) # if search char is NOT found at index
- except TypeError:
- print("ERROR: index: ", index, " is not a string!") # this is for catching errors, should never trigger
- index = index + 1 # incriment the counter by 1
- sys.stdout.write(("instances of " + searchChar + " were found at these indexes: "))
- for i in indexList: # write all index at whcih searchChar were found
- sys.stdout.write((str(i) + ", "))
- sys.stdout.write("\n") # newline
- foo = input("press enter to exit") # pause
Advertisement
Add Comment
Please, Sign In to add comment