1

Just to clarify, I know next to nothing in python 3 so searching gave me answers that were still difficult for me to understand

I am following a tutorial, a memory game in python 3

I have this piece of code written so far:

board_invisible = list("ABCD") * 2
board_visible = list("________")
revealed = []

while True:

    print(" ".join(board_visible))
    print("0 1 2 3 4 5 6 7")


    user_input = input()

    if user_input == "q":
        break

    if len(revealed) == 2:
        for i in revealed:
            board_visible[i] = '_'

            revealed = []

    board_index = int(user_input)
    board_visible[board_index] = board_invisible[board_index]

    revealed.append(board_index)

My assignment was to find a way to catch any user input that is not a number from 0 to 7 and print wrong input if that was the case.

Now, I have seen a few solutions for doing this, but beforehand, I had came up with my own solution, which did not work. It was as follow :

if user_input == "0" or "1" or "2" or "3" or "4" or "5" or "6" or "7":
        continue
    else:
        print("wrong input")

I had integrated it like this :

board_invisible = list("ABCD") * 2
board_visible = list("________")
revealed = []

while True:

    print(" ".join(board_visible))
    print("0 1 2 3 4 5 6 7")

    user_input = input()

    if user_input == "q":
        break

    if user_input == "0" or "1" or "2" or "3" or "4" or "5" or "6" or "7":
        continue

    else:
        print("wrong input")

    if len(revealed) == 2:
        for i in revealed:
            board_visible[i] = '_'

            revealed = []

    board_index = int(user_input)
    board_visible[board_index] = board_invisible[board_index]

    revealed.append(board_index)

The result was that python would completely ignore my "if condition" that I hoped would prevent the user from entering anything else than a number from 0 to 7.

I would like to know why this piece of code did not work for that purpose:

if user_input == "0" or "1" or "2" or "3" or "4" or "5" or "6" or "7":
    continue

else:
    print("wrong input")

I should also mention the suggested solution I had seen, which only half worked for me: (changed some variable names for simplicity's sake)

board_visible = list("________")
board = ['A', 'B', 'C', 'D'] * 2 
flipped = []

while True:

    print(" ".join(board_visible))
    print("0 1 2 3 4 5 6 7")

    if len(flipped) == 2:
        for i in flipped:
            board_visible[i] = '_'
        flipped = []

    user_input = input()

    if user_input == "q":
        break

    if user_input.isdigit():

        idx = int(user_input)

        if idx < len(board):

            board_visible[idx] = board_invisible[idx]

            flipped.append(idx)

            continue

        print("wrong input")

but for some reason it only wrote "wrong input" when I entered an "out of range number". Entering a character (anything else than an int) simply continued to run my program as if I did not entered anything.

the last line "print("wrong input") has a wrong indentation. now it works, but I still am glad that I could learn that there is more than one way to do this, thanks to the answers given in this thread.

2
  • You should clarify about "did not work", please explain the behavior. My guess (on a mobile so can't prove it) is that you are calling continue which would advance to the next iteration of your while loop. Meaning you'd want to do something more like if user_input not in [x for x in range(10)]: print("message"); continue (minimized and not necessarily the best solution, but at least gives an idea). Commented Jan 14, 2015 at 4:20
  • "did not work" meant that python would simply ignore the problematic "if condition". For example, I would input "9" and the message "wrong input was not printed as I had hoped. There was not error message either, the code would execute just as if I had never entered these lines of code. but I see what you are trying to explain except I am not sure how I would use that for my program. I try looking into that some more Commented Jan 14, 2015 at 4:33

1 Answer 1

2

That condition returns True if user_input == "0" and returns "1" if user_input != "0" because == has more precedence than or. And because that how or works, it returns the first operand if it is "truthy" (non empty lists, non empty strings, True, etc...) else it returns the second operand.

Example:

 In [8]: 1 == 1 or "1" or "2" or "3" or "4" or "5" or "6" or "7"
 Out[8]: True

 In [9]: 1 == 0  or "1" or "2" or "3" or "4" or "5" or "6" or "7"
 Out[9]: '1'

You have different ways to check if the input is what you want:

 if user_input in "01234567": # ...
 if user_input in [ "0", "1", "2", "3", "4", "5", "6", "7" ]: # ...

 import re
 #...
 if re.match(r'^[0-7]$', user_input): # ...

Also note the body of your if is incorrect, continue skips the rest of the current while iteration, so if the user inputs a value between 0 and 7 the program just returns to the beginning of the while.

You should write something like:

 if user_input not in "01234567": # Notice the `not in` operator
     print("wrong input")
     continue # To skip the rest of the while and ask the user to input a number again

You may understand the semantics of continue with the following example:

 # Lets print even numbers
 for i in range(20):
     if i % 2 != 0: # if it is odd
         continue   # skip the rest of this for
     print(i)       # prints i (only even numbers reach this point)      
Sign up to request clarification or add additional context in comments.

4 Comments

I did not fully understand your answer. (because I cannot see how I would be able to insert this into my program example) but I can see that I had the wrong idea about how "or" works. I'll try to read some more about it
I have edited the answer, there were two errors, one was the implementation of the condition, and the other is that continue doesn't do what you expect.
Checking the comments of the question nerdwaller detected the problem with continue before me :)
wow this edited comment of yours really did the trick! I get it completely now. Before I had read your answer, I was gonna go with my solution that I edited in. but I like what you suggest

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.