0
votes

I am writing a python game and it has following features to ask from user.

  1. it can be up to 4 players (minimum 1 player, maximum 4 player)
  2. It will ask players name. If the name is already exists, the program will prompt "name already in the list" and ask to enter the name again
  3. if the player enters empty string in player name input, it will exits.
  4. it will ask how many n number of random digits player want to play with (randint(start, stop) is used). only up to 3 digits are allowed

I know I have to user while loop for indefinitely ask the user input until the condition is satisfied. I also have to use for loop to ask users for a name based on the input at point 1.

Following is my attempt which has errors. Hence, need your help in review -

def attempt1():
playerList = []
numPlayers = input("How Many Players? ")
if int(numPlayers) < 5 and int(numPlayers) > 0:
    while True:
        if numPlayers != "":
            for i in range(int(numPlayers)):
                playerName = input("Player name or <Enter> to end ")
                if playerName != "":
                    if playerName not in playerList:
                        playerList.append(playerName)
                    break
                else:
                    print("Player Name Cannot be empty")
                    # numPlayers = input("How Many Players? ")
        else:
            print("There must be at least one player")
            numPlayers = input("How Many Players? ")
else:
    print("Invalid number of players. Please enter 1 - 4")
print(playerList)

def attempt2(numPlayers):
playerList = list()
# numPlayers = 1
i = 0
while i < 4:
    for x in range(0,numPlayers):
        playerName = input("Name ")
        if playerName not in playerList:
            playerList.append(playerName) 
            i += 1
        else:
            print("Name is already in the list")
print(playerList)
return playerList
1
what errors are you getting can you post the errordeadshot
better write function which ask name only one player - and later use it in loop which runr it for 4 players.furas
you could do numPlayers = int(numPlayers) at start and then you don't have to repeate int(numPlayers) so many times. Code will be more readable.furas
@furas So I write a function to ask username 1 time and use it inside while loop and for loop? will it suit for my point number 2?Austin
@deadshot for attempt2, my program will prompt user input 4 times if numPlayers = 4 and if I input non-duplicate names. But it will prompt more than 4 times if I input duplicate name. so my len(playlist) becomes 8 which is supposed to be minimum 1 or maximum 4Austin

1 Answers

0
votes

This solves my issue. Please kindly suggest if you have better solutions. I am sure my code here is messy but it works for now.

def askPlayerName(numPlayers):
playerList = []
x = numPlayers
while True:
    if x > 0:
        for i in range(x):
            print(x)
            playerName = input("Enter name: ")
            if playerName not in playerList:
                x -= 1
                playerList.append(playerName)
                break
            else:
                print("ELSE")
                x = numPlayers - len(playerList)
                print(x)
                print("name aldy in the list")
    else:
        return playerList
return playerList