Check if string only contains characters from list of characters/symbols?

前端 未结 4 1770
野的像风
野的像风 2021-01-28 03:44

How can I check in Python 3 that a string contains only characters/symbols from a given list?

Given:

  • a list allowedSymbols = [\'b\', \'c\', \'z\', \'
4条回答
  •  半阙折子戏
    2021-01-28 04:30

    This should do it.

    for i in enteredpass:
        if i not in allowedSymbols:
             print("{} character is not allowed".format(i))
             break
    

    Not sure what you're looking for with the Score = score -5. If you want to decrease score by 5 if all entered characters are in the allowedSymbols list just put score = score - 5 on the same indentation level as the for loop, but at the end of the code after the if block.

提交回复
热议问题