0

I'm wondering why my function does not call the index of the character in the string. I used a for loop for this, and for some reason it's just listing all of the possible indices of the string. I made a specific if statement, but I don't get why it's not following the instructions.

def where_is(char,string):
  c=0
  for char in string:
    if char==(string[c]):
      print (c)
      c+=1
    else:
      print ("")
where_is('p','apple')
5
  • Please always include sample input, the result and how it differs from your expected output. Commented Mar 31, 2017 at 16:22
  • 1
    You don't increase the index in else part. Commented Mar 31, 2017 at 16:22
  • 1
    @Kasramvd I don't think the else part ever executes, so that's not an issue :) Commented Mar 31, 2017 at 16:23
  • 1
    @timgeb Yes, it seems that the name of the throwaway variable in for loop is also char. Commented Mar 31, 2017 at 16:25
  • 3
    There's a couple issues with your code. Get a rubber duck and explain to it, in detail, what each line is supposed to do and why it is obviously correct. If you can't, that's where one of the bugs is. Commented Mar 31, 2017 at 16:26

4 Answers 4

1

Your loop is overwriting your parameter char. As soon as you enter your loop, it is overwritten with a character from the string, which you then compare to itself. Rename either your parameter or your loop variable. Also, your counter increment c+=1 should also be outside of your if. You want to increase the index whether or not you find a match, otherwise your results are going to be off.

And just as a matter of style, you don't really need that else block, the print call will just give you extra newlines you probably don't want.

Sign up to request clarification or add additional context in comments.

Comments

0

Firstly, the index you used is not being increased in the else part and secondly, I generally prefer a while loop to a for loop when it comes to iterating through strings. Making slight modifications to your code, have a look at this :

def where_is(char,string):
  i=0
  while i<len(string):
    if char==(string[i]):
      print (i)
    else:
      print ("")
    i+=1
where_is('p','apple')

Input : where_is('p','apple')

Output: 1 2

Check it out here

Comments

0

The problem is that the given code iterated over everything stored in the string and as it matched everytime the value of 'c' increased and got printed. I think your code should be:

def where_is(char, string):
    for i in range(len(string)):
        if char == (string[i]):
            print(i)

where_is('p', 'apple')

This prints the index of all the 'p's in 'apple'.

Comments

0

As mentioned in the comments, you don't increment correctly in your for loop. You want to loop through the word, incrementing each time and outputting the index when the letter is found:

def where_is(char, word):
    current_index = 0
    while current_index < len(word):
        if char == word[current_index]:
            print (current_index)
        current_index += 1

where_is('p', 'apple')

Returning:

1
2

Alternatively, by using enumerate and a list comprehension, you could reduce the whole thing down to:

def where_is(char, word):
    print [index for index, letter in enumerate(word) if letter == char]

where_is('p', 'apple')

which will print:

[1,2]

You then also have the option of return-ing the list you create, for further processing.

Comments

Your Answer

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