python - Checking if a value is equal to any value in an array -


this question has answer here:

i'm new python (and programming in general) , can't seem find solution myself. want check first letter of string equal letter stored in array, this:

letter = ["a", "b", "c"] word = raw_input('enter word:') first = word[0]  if first == letter:     print "yep" else:     print "nope" 

but doesn't work, know how will? in advance!

you need use in operator. use if first in letter:.

>>> letter = ["a", "b", "c"] >>> word = raw_input('enter word:') enter word:ant >>> first = word[0] >>> first in letter true 

and 1 false test,

>>> word = raw_input('enter word:') enter word:python >>> first = word[0] >>> first in letter false 

Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -