rodpadev
rodpadev

Reputation: 393

check if any character of a string is inside a list

This is a Pig latin game and it works but right now if I input any of the characters defined by the variable "special" I get it returned as True but if I add another character it returns as false.


So it's only checking if the whole string and not any character of the string. I want that if I input "Ban/", it gets returned as invalid. So if any of the characters defined in "special" are found in "word" I want it to return true.

special = list('[@_!#$%^&*()<>?/\|}{~:]')
word=input("\nType in a word : ")
if word in special:
    print("Your entry is not valid.")
else:
    pigLatin()

i also tried this before but it's essentially the same

if word[0:] in special:

Here's the full snipet if it helps. Condition is in Line:41

Beware I started a few days ago so it may look really messy.

Upvotes: 2

Views: 977

Answers (1)

Primusa
Primusa

Reputation: 13498

Loop through each character in the input and perform the check one by one. You can do this with any() and a generator comprehension:

if any(i in special for i in word):
    print("Your entry is not valid.")

You may also consider using sets by checking if the intersection between word and special isn't empty:

if set(word) & set(special):
    print("Your entry is not valid.")

Upvotes: 1

Related Questions