a_lst = ['chair','gum','food','pizza']
letter = 'x'
for word in a_lst:
if letter not in word:
print ('no',letter)
elif letter in word:
print ('yes',letter)
輸出:檢查字符串中的charcters和outputing單個消息
no x
no x
no x
no x
有沒有一種方法,我可以遍歷雖然「a_lst」每個項目,檢查每個項目都有字母「X」。如果沒有項目有字母'x'打印'沒有x'只是一次。如果一個單詞包含字母'x',只需打印'是x'一次。
我覺得我的邏輯在某個地方是有缺陷的。
有什麼建議嗎?
感謝