我想用一行代码检查它们。可能吗?否则,我目前的代码是:
while True:
player_number = int(input('Determine first player Random(0) or AI(1):'))
player_number2 = int(input('Determine second player Random (0) or AI (1):'))
if player_number, player_number2 in range(0,2): # shows error
break
else:
print ('Out of range. Please choose a player')
答案 0 :(得分:3)
我只会使用and
:
if player_number in (0, 1) and player_number2 in (0, 1):
您也可以使用套装,但不太清楚:
if {player_number, player_number2} <= {0, 1}:
答案 1 :(得分:1)
您可以使用all
声明:
if all(i in range(0, 2) for i in (player_number, player_number2)):
这种方法的好处是可以轻松扩展到多个玩家。
更好的选择是使用词典并与集合进行比较:
while True:
players = {k: int(input('Determine player {0} Random(0) or AI(1):'.format(k)))\
for k in range(0, 2)}
if set(players.values()) <= {0, 1}:
break
else:
print('Out of range. Please choose a player')