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.
continuewhich would advance to the next iteration of yourwhileloop. Meaning you'd want to do something more likeif 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).