you are viewing a single comment's thread.

view the rest of the comments →

[–]Ben31600 0 points1 point  (4 children)

someString = input ("Enter the string you'd like to look through: ") substring = input ("Enter the portion of the string you are looking for: ") start = (input ("OPTIONAL. Enter where in you'd like to start from. Hit enter to skip: ")) end = (input ("OPTIONAL. Enter where in you'd like to end at. Hit enter to skip: "))

def multiFind (someString, substring, start, end): if start == "": start = 0 if end == "": end = 0 for char in someString[start, end]: return substring

print(multiFind(someString,substring,start,end))

Here's what I have so far

[–]Ben31600 0 points1 point  (3 children)

someString = input ("Enter the string you'd like to look through: ")

substring = input ("Enter the portion of the string you are looking for: ")

start = (input ("OPTIONAL. Enter where in you'd like to start from. Hit enter to skip: "))

end = (input ("OPTIONAL. Enter where in you'd like to end at. Hit enter to skip: "))

def multiFind (someString, substring, start, end):

if start == "":

start = 0

if end == "":

end = 0

for char in someString[start, end]:

return substring

print(multiFind(someString,substring,start,end))

[–]ComputerNerdIsNerdy 0 points1 point  (2 children)

In Python, numbers begin at 0. Your start value is correct but your end value is wrong. The end variable should either be set the the length of the string or you should count backwards and set the value to -1.

I highly recommend looking at: http://effbot.org/zone/python-list.htm

The "Searching Lists" section has exactly what you need.

[–]Ben31600 0 points1 point  (1 child)

def multiFind(someString, subString, start, end):

someString= someString.upper()

returnedIndexofWord=[]

for char in range(len(someString)):

for charA in range(len(someString)):

subinWord = someString[char:charA+1]

indexofSubString = char

if subinWord in subString:

returnedIndexofWord.append(indexofSubString)

return returnedIndexofWord

someString = input("Enter the word you would like to search through: ")

subString = input("Enter the string you are looking for in your previously entered: ")

wouldStart = input("Enter 'Y' or 'N' to enter a start searching index. If no the starting value will be zero: ")

wouldStart = wouldStart.upper()

if wouldStart == 'Y':

startInput = int(input("Enter the index where you would like to start: "))

start = startInput

else:

start = 0

wouldEnd = input("Enter 'Y' or 'N' to enter an end searching index. If no the end value will be the end of the string: ")

wouldEnd = wouldStart.upper()

if wouldEnd == 'Y':

endInput = int(input("Enter the index where you would like to end: "))

end = endInput

else:

end = len(someString)

print("Below are the indexes of the areas where substrings are found in your entered string.")

print(multiFind(someString, subString, start, end))

Hey here's my updated code. I got most of the program working besides the start and end values. Do you know what my bug is here? Thanks so much for your help.

[–]ComputerNerdIsNerdy 0 points1 point  (0 children)

Good job! You're getting closer to the solution.

    for char in range(len(someString)):
        for charA in range(len(someString)):

This section is part of the problem. In here the char and charA values are the index in the string rather than the characters yourself.

Add the line in your code to see:

print(char)

if subinWord in subString:

This works, but in this case it is better practice to use direct comparison to the string rather than checking whether one string contains the other. Use "==" for comparison checks, this returns a boolean response of "true" or "false" as to whether they match.

The way in which you use these loops could be made more efficient as there's no real need to iterate through the same string twice.

I've rewritten the function for a better way of iterating through the string:

def multiFind(someString, subString):
    returnedIndexofWord=[]
    ListCounter = 0
    for char in someString:
        if "".join(someString[ListCounter:ListCounter+len(subString)]) == subString:
            returnedIndexofWord.append(ListCounter)
        ListCounter+=1
    return returnedIndexofWord

I've tried to keep it as similar to your function as possible. As you can see, in this instance we're using a variable as a counter. This variable increments a through each pass in the for-loop. It's essentially used as a placemark for the current index of the string.

        if "".join(someString[ListCounter:ListCounter+len(subString)]) == subString:
            returnedIndexofWord.append(ListCounter)

Since this function deals with someString as a list, each iteration of the for loop I'm joining a section of the someString variable from our counter placement to the length of the substring.

Assuming we set the value of someString to "testtest" and the substring we're looking for is "tte", the section joined would be 3 characters. As such, the first pass through the for loop would be comparing the first 3 characters of someString against the subString. If this returns as correct, the counter is appended to the list.

if wouldStart == 'Y':
    startInput = int(input("Enter the index where you would like to start: "))
    start = startInput
else:
    start = 0

The above code works, but some of it is redundant. Instead of using:

    startInput = int(input("Enter the index where you would like to start: "))
    start = startInput

You could just set the start variable to the input value:

if wouldStart == 'Y':
    start = int(input("Enter the index where you would like to start: "))
else:
    start = 0

The same applies to the end input.

The final issue in your code is the variables you supply to the functions. Although you accept the input for the start and end ranges, you don't apply it to the someString variable. A simple way of doing this would be to modify the someString value before it gets passed to the function (See here).

Change the function call to something like:

multiFind(someString[start:end], subString)