-1

I am finding it increasingly difficult to find the letters from a word that the user generated. The code I am using is on linear searches and can only display letters I have inputed. Edit: To improve my question would like to know if I could search for a letter from the wordlist created in choice 1.

if choice == "1": print ("Enter a Word") minput= input()

    wordList= list (minput)
    print (wordList)
    menu()

if choice== 2

letter=('a,b,c,d,e,f,g,h,f,h') print () counter=0 searchLetter=input('Enter letter to find\t')

while counter<len(letter) and searchLetter!=letter[counter]:
    counter+=1
if counter <len(letter):
    print(searchLetter,'found')
else:
    print(searchLetter, ' Not found') 
Radar
  • 17
  • 5
  • Possible duplicate of [Count occurrence of a character in a string](https://stackoverflow.com/questions/1155617/count-occurrence-of-a-character-in-a-string) – Jalo Nov 04 '17 at 11:19
  • @Jalo: No, it searches the index of a letter in a sequence.Similar to: `letter.index(searchLetter)`. – Laurent LAPORTE Nov 04 '17 at 13:05
  • @LaurentLAPORTE Why do you know that he is looking for the index of the letter? – Jalo Nov 04 '17 at 13:40
  • @Jalo: This is what the code do (imperfectly). – Laurent LAPORTE Nov 04 '17 at 13:50
  • @LaurentLAPORTE What I think, is that he is simply checking if the loop finished checking the whole string in order to stop the loop, but not for getting the letter index. But I also think that there is no point in discussing about this, as the OP is very poorly written. – Jalo Nov 04 '17 at 14:02

2 Answers2

0

You can use in parameter. if letter in word: print("word include that letter")

0

As a commented, you can use the index() function to find the index of a letter in a string, like this:

letter = 'a,b,c,d,e,f,g,h,f,h'
searchLetter=input('Enter letter to find\t')
try:
    index = letter.index(searchLetter)
except ValueError:
    print("Letter not found")
else:
    print('Letter found in index {0}'.format(index))

Here, I use a exception handler to check if the input exists in the string.

You can also use a condition, like this:

if searchLetter in letter:
    ...
Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103