Ad

Game Of 6 Players

- 1 answer

I want the user to choose two players from a menu of 6 players, these players will play a game of tennis.

I tried making a menu with submenues but it just gets complicated and messy.

def player1():
    print("You chose player 1, please choose another player")
def player2():
    print("You chose player 2, please choose another player")
def player3():
    print("You chose player 3, please choose another player")
def player4():
    print("You chose player 4, please choose another player")
def player5():
    print("You chose player 5, please choose another player")
def player6():
    print("You chose player 6, please choose another player")


Player_select = {
    1: player1,
    2: player2,
    3: player3,
    4: player4,
    5: player5,
    6: player6
}

Selection = 0


while (Selection != 7):
     print("1. player1")
     print("2. player2")
     print("3. player3")
     print("4. player4")
     print("5. player5")
     print("6. player6")
     print("7. Quit")
     Selection = int(input("Please choose two players: "))
     if (Selection >= 0) and (Selection < 7):
         Player_select[Selection]()
     Selection = Selection - 1

The results is that the while loop continues longer then i want to. I want the user to choose two players meaning two iterations? also I want the player the user chooses to not appear anymore as a choice in the second iteration.

Ad

Answer

def player1():
    print("You chose player 1, please choose another player")
def player2():
    print("You chose player 2, please choose another player")
def player3():
    print("You chose player 3, please choose another player")
def player4():
    print("You chose player 4, please choose another player")
def player5():
    print("You chose player 5, please choose another player")
def player6():
    print("You chose player 6, please choose another player")


Player_select = {
    1: player1,
    2: player2,
    3: player3,
    4: player4,
    5: player5,
    6: player6
}

Selection = 0

count = 0
while (count < 2):
    count += 1
    for k in Player_select:
        print(str(k) +". player"+str(k))
    Selection = int(input("Please choose two players: "))
    if (Selection >= 0) and (Selection < 7):
        Player_select[Selection]()
        del Player_select[Selection]

This deletes the player from the dictionary once selected. Can you tell me why you decremented the Selection by 1 at the end of the while loop? If I type 7 to exit, the loop does 7 - 1 and so doesn't exit.

Ad
source: stackoverflow.com
Ad