Print two appearances of character found in python list
First time here (and a programming noob), hope I get the formatting correct!
I'm trying to make a function that will print out where in a list the
occurence of a sought after letter is placed. The code below finds the
letter and prints out where in the list the letter is i.e. if you search
for 'a' the program will answer it's in the 2nd spot (x+1).
The problem is, if I search for a letter that have more than one
occurrencies (for example the letter 'e'), the program finds the letter in
both spots but in both cases prints out that it is in the 10th spot.
I'm trying to find out why, should be 10th and 17th in this case.
# store string in variable
solution = list('can you guess me')
guess = raw_input('What letter do you guess on? ')
# Search list
def search(guess):
nothing = 0
for x in solution:
if x == guess:
print x,
print "is in ",
print solution.index(x) + 1
nothing = 1
if nothing == 0:
print "Couldn't find ",
print guess
search(guess)
If choosing e, like this:
What letter do you think is in the answer? e
the program prints out:
e is in 11
e is in 11
I would like to know why. :/
No comments:
Post a Comment