[Solved] I want it to give me error when is not 0 or 1 [closed]


I would do

if any((i != 0 and i != 1) for i in state):
  raise ValueError("Error: A state is not specified or invalid entry")

This is short and concise.

It can be even changed to

valid = set((0, 1))
if any(i not in valid for i in state):
  raise ValueError("Error: A state is not specified or invalid entry")

bit I am not sure if that is better performance- or readability-wise.

solved I want it to give me error when is not 0 or 1 [closed]