If you want to know if any of the words contained in claves
is in the string just entered you must go through the list generated by str.split
and go checking one by one each word:
print("Prueba para determinar palabras")
claves = ['hambre', 'comida']
msj = input("Digita una oracion: ")
mensaje = msj.split()
for palabra in mensaje:
if palabra in claves:
print("Mañana te cocino")
break
else: # Si el ciclo se ha completado (no se ha ejecutado break)
print("Ok")
input()
Since I suppose you do not intend to count the number of times a forbidden word appears, it is a good idea to break the cycle at the moment when a match is found ( short circuiting ), for this use break
.
Another way to do this is to use the built-in any
next to a generator that also iterates over the list and performs the membership check with in
. any
returns True
if at least one of the values of the iterable is evaluated as true.
On the other hand, if keys instead of a list is a set ( set
) the process will be considerably more efficient when using a hash table for the search, instead of having to go through the entire list claves
complete every time you do palabra in claves
:
print("Prueba para determinar palabras")
claves = {'hambre', 'comida'}
msj = input("Digita una oracion: ")
mensaje = msj.split()
if any(palabra in claves for palabra in mensaje):
print("Mañana te cocino")
else:
print("Ok")
input()