I am writing a python game and it has following features to ask from user.
- it can be up to 4 players (minimum 1 player, maximum 4 player)
- 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
- if the player enters empty string in player name input, it will exits.
- 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
numPlayers = int(numPlayers)
at start and then you don't have to repeateint(numPlayers)
so many times. Code will be more readable. – furas