The more Pythonic way is to use all()
, it's faster, shorter, clearer and you don't need a loop:
allowedSymbols = ['b', 'c', 'z', ':']
enteredpass1 = 'b:c::z:bc:'
enteredpass2 = 'bc:y:z'
# We can use a list-comprehension... then apply all() to it...
>>> [c in allowedSymbols for c in enteredpass1]
[True, True, True, True, True, True, True, True, True, True]
>>> all(c in allowedSymbols for c in enteredpass1)
True
>>> all(c in allowedSymbols for c in enteredpass2)
False
Also note there's no gain in allowedSymbols
being a list of chars instead of a simple string: allowedSymbols = 'bcz:'
(The latter is more compact in memory and probably tests faster too)
But you can easily convert the list to a string with ''.join(allowedSymbols)
>>> allowedSymbols_string = 'bcz:'
>>> all(c in allowedSymbols_string for c in enteredpass1)
True
>>> all(c in allowedSymbols_string for c in enteredpass2)
False
Please see the doc for the helpful builtins any()
and all()
, together with list comprehensions or generator expressions they are very powerful.