0

I'm trying to do an assignment where I have to find the amount of letters before it gets to a "f" in a string. I was able to get one thing to work, but it only prints out one "f".

An example of this would be "office", it takes 1 letter to get to one of the "f's", but I can not get it to get to the second f, if that makes sense.

a = input()
b = a.index("f")

print(b)
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
reedmeem
  • 45
  • 3

2 Answers2

0

Here is a mode to get all letters util the last "f", but that also will include all letters "f's" before.

To get the amount of letters util the last "f".

a = str(raw_input())

def find(str, char):
    values = []
    for i, ltr in enumerate(str):
        if ltr == char:
            values.append(i)    
return max(values)

# >>> print(find(ooooffooof, "f"))
# >>> 9
Yago Azedias
  • 4,480
  • 3
  • 17
  • 31
0

Evert time you find that char you are looking for again change the current position to your index in the string.

import sys

def getNumLettersBefore(string, c):
    position = 0
    for i in range(len(string)):
        if string[i] == c:
            position = i
    return position


string = sys.argv[1]
c = sys.argv[2]
a = getNumLettersBefore(string, c)
print a