我不明白"while "语句和此代码中"if player:"语句的必要性



我不明白"而";语句和语句";如果玩家:";在这个代码-

def get_players_list():
"""() -> list of [str, int] list
Prompt the player(s) to enter their names and return a list of player info
as a two-item list with name and score, respectively.
"""
players = []
player = input('Enter player 1 name: ')
while player.strip() or not players:  ##**#why is this needed?**
player = player.strip() 
if player in players:
print("A player by that name is already playing.")
if player: **# what is the function of this command#**
players.append([player, 0])
if players:
print('Leave a blank player name to begin playing.')
player = input('Enter player {num} name: '.format(num=len(players) + 1))
return players

这似乎是一个死块,因为"玩家";是列表的列表,但是";玩家";是字符串:

if player in players:
print("A player by that name is already playing.")

所以"如果玩家中的玩家:";这种情况永远不会发生。

关于"if"块,如果您的输入不是空白,则需要将元素添加到"players"列表中。只有当"players"为空时,检查"if"条件才很重要,在其他情况下,它总是True。"while"使您至少输入1个有效输入。

最新更新