Question:
checking for specific letters in a string, python?
blah
2010-07-07 00:38:33 UTC
This is what I want to do: take an input, check if any of the letters in the input match with the secret word. So the secret word is "apple", the user guesses "ppapp". So the matching letters are "a", "p", and "p".Total 3 correct letters. Let's say the user guesses "paaaa", the matches are "p" and "a", so total correct is 2. I do not want to take location of letter into account.

#this is what I have
secretWord = "apple"
guess = "ppapp"

for i in guess:
a = codeWord.replace(i,"") #stores letters which have not been guessed
b = len(secretWord) - len(a) #takes the length of original word, minus length of 'a',
# giving number of correct guesses
print a
print b
print len(a)

#output:
ale
2
3
#end

What I wanted to do was replace matching inputted letters with "", which would essentially delete the letter. Than take the length of the secret word and subtract with the letters not matched (I used variable 'a' in the code). At the end, I get the number of correct letters inputted by the user.

I've been stuck for a while, I've tried re-writing it different ways, but can't get it. I need help! but it needs to be on the more basic side, I'm taking my first course in python
Three answers:
om
2010-07-07 10:17:34 UTC
The main problem here is that this program doesn't keep track of the successive letter removals that you're trying to perform on the secret word. Every time around the loop you remove the next guess letter from the original secret word, but then you throw away the result. You should start off by setting the 'a' variable to be a copy of the secret word, and then each time around the loop you should remove the next guess letter from 'a', not from the original secret word. (BTW, I assume 'codeWord' is a typo, it should be 'secretWord'.)



This is why the final result is 2 -- it's because the final letter in the guess is 'p', so the final trip through the loop removed the two p's from 'apple' leaving 'ale'. The results of examining any earlier letters in the guess are lost. This brings up the second problem: the call to the replace() method in this program removes *all* occurrences of the given guess letter from the secret. You only want to remove one occurrence each time around the loop, so you need to pass a third argument (the maximum number of occurrences of the letter to remove) to that call.



When you fix those issues you should end up with something like this:



  a = str( secretWord ) # a is a copy of secretWord

  for i in guess:

      a = a.replace( i, "", 1 )

  b = len( secretWord ) - len( a )
2016-12-18 01:21:35 UTC
def uses_only(observe): for char in observe: if char != observe.locate(char): return observe fin = open( observe.txt ) for line in fin: observe = line.strip() word_wo_same_letter = uses_only(observe) print word_wo_same_letter
2016-12-07 12:46:08 UTC
def uses_only(word): for char in word: if char != word.discover(char): return word fin = open( word.txt ) for line in fin: word = line.strip() word_wo_same_letter = uses_only(word) print word_wo_same_letter


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...