0

I am just learning python and what I am trying to do is take an input from a user and then check if the values in that input are in a list of acceptable inputs.

This is what I have so far:

start = []
while len(start) < 9 or len(start) > 9 and all(elem != 'X' or 'O' or '_' for elem in start):
    start = list(input())
    if len(start) < 9:
        print("Length needs to be at least 9 symbols.")
    elif len(start) > 9:
        print("Please only input 9 symbols.")
    elif any(elem != 'X' or 'O' or '_' for elem in start):
        print("Please only use symbols X, O, _")

When I run this, everything works fine for the length but as soon and I try to see if the values in start equal (X, O, or _) is where it all falls apart. If the first two statements in the while loop are satisfied it will always print the 3rd if statement and then exit the loop.

What am I missing?

azro
  • 53,056
  • 7
  • 34
  • 70
  • https://stackoverflow.com/questions/6838238/comparing-a-string-to-multiple-items-in-python – azro May 30 '20 at 19:01
  • 2
    `elem != 'X' or 'O' or '_'` is different of `elem != 'X' and elem != 'O'and elem != '_'` You cannot jsut do `or` to test against other string, you have to repeat elem!= – azro May 30 '20 at 19:02
  • `and all(elem in 'XO_' for elem in start):` tests if all elements are O or X or _. If you want the opposit do `any(elem not in 'XO_' for elem in start)` or simply `not all(elem in 'XO_' for elem in start)` – Patrick Artner May 30 '20 at 19:11
  • if you want you could also do `allowed_set=frozenset("XO_")` and then test `set(start) == allowed_set` – Patrick Artner May 30 '20 at 19:12
  • None of your examples have worked. My last elif statement will always make the while loop exit even though the while statement is still true. All your examples react as if ALL of the elems in the set 'XO_' are in start then its a valid statement. I want to check to see if ALL the values in start and is if they are 'X', or "O', or '_'. – Ernie Wolf May 30 '20 at 21:03

0 Answers0