【问题标题】:Assigning player in an interactive game在互动游戏中分配玩家
【发布时间】:2022-11-25 04:43:53
【问题描述】:

我正在为一个简单的游戏制定解决方案,您可以选择最多 5 名玩家和最少 2 名玩家的数量。每个玩家都由他们的名字和姓氏标识。

max_players = int(input(" Insert the number of players there are there? : "))
while len(players_list) < max_players:
    player1 = input(" What is your first and last name? : ")
    players_list.append(players)
    print("players so far : ")
    player2 = input ("What is your first and last name? :")
    players_list.append(players)
    print("players so far : ")
    print(players_lists)

该代码部分起作用。问题是,虽然我提到了玩家的最大数量,但你仍然可以使用大于 5 的数字。另外,当我插入玩家的名字时,它显示“玩家”没有定义? 预期的输出是

Hello how many players are in the game?

insert number between 2 and 5

Please insert your name

然后对其他玩家执行相同的命令。

【问题讨论】:

    标签: python list


    【解决方案1】:

    另外,当我插入玩家的名字时,它显示“玩家”未定义

    您尝试将 players(在您调用它之前确实不存在)附加到 players_list(同样不存在)。您需要先定义这两个。

    对于玩家数量限制,您可以添加一个简单的检查,max_players 是否在 2 到 5 之间。

    max_players = int(input(" Insert the number of players there are ? : "))
    while (max_players <2) or (max_players > 5) :
        max_players = int(input(" Number of players must be between 2 and 5. Number of players ? "))
    
    players_list = []
    while len(players_list) < max_players:
        player1 = input(" What is your first and last name? : ")
        players_list.append(player1)
        print("players so far : ")
        print(players_list)
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-14
      • 1970-01-01
      相关资源
      最近更新 更多