希望选择2中的代码搜索从选项1创建的单词中的字母。我似乎无法找到解决方案。
if choice==1:
print ("Enter a Word")
minput= input()
wordList= list (minput)
print (wordList)
mainMenu()
if choice==2:
letter=['a,b,c,d, e, f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z']
counter=0
searchletter=input('Enter letter to search\t')
while counter<len(letter) and searchletter!=letter[counter]:
counter+=1
if counter <len(letter):
print(searchletter,'found')
else:
print(searchletter, ' Not found')
mainMenu()
答案 0 :(得分:0)
您的问题出在while
循环中。目前,在搜索时,您的循环将退出第一个不是您的搜索字母的字符。相反,你可以简单地使用
print("found" if searchletter in word else "not found")
并将您的单词的变量名称放在word
。
>>> word = "testme"
>>> searchletter = "t"
>>> print("found" if searchletter in word else "not found")
found
>>> searchletter = "f"
>>> print("found" if searchletter in word else "not found")
not found