-3

I'm making a program in python, which has an input that only asks for the number one or the number 2, but I want to check if the response of the input is only in these two numbers, I'm using the not in operator, but it's not working, because I want to check while the input is not at one or number two, it will ask again, code dowm

while testamento not in "1" or not in "2":
    testamento = input("Digite denovo: ")```

what can i do to check the two numbers?
VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • 1
    Have you tried `while testamento not in ["1", "2"]:` ? – Nick ODell Mar 19 '22 at 04:26
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Mar 19 '22 at 09:19

1 Answers1

0

You can create a set of acceptable strings, then check whether the input string is in that set:

testamento = None
while testamento not in {"1", "2"}:
    testamento = input("Digite denovo: ")

(A previous answerer suggested calling the dunder method __contains__ directly, rather than using the in operator, which you shouldn't do. I was going to include this in a comment to their answer, but it was deleted before I could post the comment.)

BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33