How to make input verification in a separate function? the program should take the input sequence 1 and 0
def check(SEQ):
for i in SEQ:
isinstance(i, int)
if not (set(SEQ) == {0, 1} or set(SEQ) == {1} or set(SEQ) == {0}):
return False
else:
return True
def main():
SEQ = [i for i in input("Enter the sequence 0 and 1 ").split()]
while not check(SEQ):
print("Invalid values ")
SEQ = [i for i in input("Enter the sequence 0 and 1 ").split()]
if __name__ == '__main__':
main()
SEQ = list(map(lambda x: int(x), SEQ.split)). Then check ifall(map(lambda x: x==1 or x==0, SEQ)){0,1}? Also -- you don't needif ... elseto convert a boolean into a boolean. Just return the comparison itself. Those 4 lines in the function can be replaced by the 1 linereturn set(SEQ) <= {0,1}(fine print: this comparison would validate the empty string, but your original code would not. Since the empty string is a binary sequence, albeit a trivial one, this is a feature of my suggestion rather than a bug).