Python3:特定用户输入检查
Python 3: Specific user input check
澄清一下,我对 python 3 几乎一无所知,所以搜索给了我一些我仍然难以理解的答案
我正在学习 python 3
中的一个记忆游戏教程
到目前为止我已经写了这段代码:
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)
我的任务是找到一种方法来捕获任何不是 0 到 7 之间的数字的用户输入,如果是这种情况,则打印错误的输入。
现在,我已经看到了一些解决方案,但在此之前,我想出了自己的解决方案,但没有奏效。如下:
if user_input == "0" or "1" or "2" or "3" or "4" or "5" or "6" or "7":
continue
else:
print("wrong input")
我是这样集成的:
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)
结果是 python 会完全忽略我的 "if condition",我希望它能阻止用户输入 0 到 7 以外的任何数字。
我想知道为什么这段代码不能用于该目的:
if user_input == "0" or "1" or "2" or "3" or "4" or "5" or "6" or "7":
continue
else:
print("wrong input")
我还应该提到我看到的建议解决方案,它对我来说只有一半:(为简单起见更改了一些变量名称)
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")
但由于某些原因,当我输入 "out of range number" 时它只写了 "wrong input"。输入一个字符(除 int 以外的任何其他字符)只是继续 运行 我的程序,就好像我没有输入任何东西一样。
最后一行 "print("wrong input") 有一个错误的缩进。现在它可以工作了,但我仍然很高兴我可以了解到有不止一种方法可以做到这一点,多亏了答案在此线程中给出。
那个条件 returns True
if user_input == "0"
and returns "1" if user_input != "0"
因为 ==
比 or
。因为 or
的工作原理,它 returns 如果它是 "truthy" (非空列表、非空字符串、True 等),它是第一个操作数,否则它 returns 第二个操作数。
示例:
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'
你有不同的方法来检查输入是否是你想要的:
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): # ...
另请注意 if
的正文不正确,continue
会跳过当前 while
迭代的其余部分,因此如果用户输入 0 到 7 之间的值,程序只是 returns 到 while
.
的开头
你应该这样写:
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
您可以通过以下示例理解 continue
的语义:
# 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)
澄清一下,我对 python 3 几乎一无所知,所以搜索给了我一些我仍然难以理解的答案
我正在学习 python 3
中的一个记忆游戏教程到目前为止我已经写了这段代码:
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)
我的任务是找到一种方法来捕获任何不是 0 到 7 之间的数字的用户输入,如果是这种情况,则打印错误的输入。
现在,我已经看到了一些解决方案,但在此之前,我想出了自己的解决方案,但没有奏效。如下:
if user_input == "0" or "1" or "2" or "3" or "4" or "5" or "6" or "7":
continue
else:
print("wrong input")
我是这样集成的:
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)
结果是 python 会完全忽略我的 "if condition",我希望它能阻止用户输入 0 到 7 以外的任何数字。
我想知道为什么这段代码不能用于该目的:
if user_input == "0" or "1" or "2" or "3" or "4" or "5" or "6" or "7":
continue
else:
print("wrong input")
我还应该提到我看到的建议解决方案,它对我来说只有一半:(为简单起见更改了一些变量名称)
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")
但由于某些原因,当我输入 "out of range number" 时它只写了 "wrong input"。输入一个字符(除 int 以外的任何其他字符)只是继续 运行 我的程序,就好像我没有输入任何东西一样。
最后一行 "print("wrong input") 有一个错误的缩进。现在它可以工作了,但我仍然很高兴我可以了解到有不止一种方法可以做到这一点,多亏了答案在此线程中给出。
那个条件 returns True
if user_input == "0"
and returns "1" if user_input != "0"
因为 ==
比 or
。因为 or
的工作原理,它 returns 如果它是 "truthy" (非空列表、非空字符串、True 等),它是第一个操作数,否则它 returns 第二个操作数。
示例:
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'
你有不同的方法来检查输入是否是你想要的:
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): # ...
另请注意 if
的正文不正确,continue
会跳过当前 while
迭代的其余部分,因此如果用户输入 0 到 7 之间的值,程序只是 returns 到 while
.
你应该这样写:
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
您可以通过以下示例理解 continue
的语义:
# 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)