I wanted to ask how many players there are first , then depending on how many players they are playing the game, I wanted to loop through and ask their names as input and then assigning it to their player numbers. First: it will ask player numbers Second: it will ask their names Third: then it will create their names as variables
Here is what I tried but it is returning :
player_nums =int(input("how many players are there? "))
playernum = True
while playernum:
if player_nums >7 or player_nums == 0 :
print( "there cant be more than 7 players")
print("please entre the player numbers again")
player_nums =int(input("how many players are there? "))
elif player_nums == 6 :
player_one = input("please write ur name playerone")
player_two = input("please write ur name playerone")
player_three = input("please write ur name playerone")
player_four = input("please write ur name playerone")
player_five = input("please write ur name playerone")
player_six = input("please write ur name playerone")
playernum = False
elif playernum ==5:
player_one = input("please write ur name playerone")
player_two = input("please write ur name playerone")
player_three = input("please write ur name playerone")
player_four = input("please write ur name playerone")
player_five = input("please write ur name playerone")
playernum = False
elif playernum ==4:
player_one = input("please write ur name playerone")
player_two = input("please write ur name playerone")
player_three = input("please write ur name playerone")
player_four = input("please write ur name playerone")
playernum = False
elif playernum == 3:
player_one = input("please write ur name playerone")
player_two = input("please write ur name playerone")
player_three = input("please write ur name playerone")
playernum = False
elif playernum ==2:
player_one = input("please write ur name playerone")
player_two = input("please write ur name playerone")
elif playernum == 1:
player_one = input("please write ur name playerone")
playernum = False
else:
continue
print(player_one)
Here is an another method i tried but it wont work, I dont know how it is not working:
class Player:
def __init__(self,name):
self.name= name
def new_name(self):
self.new_names = input('what is your name')
def __str__(self):
return self.name_names
user = Player("")
player_nums =int(input("how many players are there? "))
playernum = True
while playernum:
if player_nums >7 or player_nums == 0 :
print( "there cant be more than 7 players")
print("please entre the player numbers again")
player_nums =int(input("how many players are there? "))
else:
for i in range(1,player_nums):
user = Player.new_name(user)
print(user.new_name)
I wanted to loop through the player number to assign individual names to their player numbers, for example if there is 5 players, Player 1 : input (his name) player 2 : input (his name) etc
Your question as written is very confusing but from what I gather you want to:
In order to perform this task in an orderly, neat and efficient manner, you should utilize the inherent data structures available to you within python.
def start_game():
while plyr_count := int(input('How Many Players are there? ')):
if plyr_count <= 7:
break
print('Number of players must be less than or equal to 7, try again')
players = []
for i in range(plyr_count):
players.append(input(f"Enter Player {i+1}'s name "))
return players
Then when executing start_game, you will get: How Many Players are there? 12 Number of players must be less than or equal to 7, try again
How Many Players are there? 4
Enter Player 1's name Bob
Enter Player 2's name Tom
Enter Player 3's name Judy
Enter Player 4's name Nancy
['Bob', 'Tom', 'Judy', 'Nancy']
f you would like to create a player class and use this in the above code this is how I would approach it.
# The player Class definition
class Player:
def __init__(self, name):
self.name = name
def __repr__(self): # Allows for printing Class
return self.name
# Update Code to utilize player class in start_game
def start_game():
while plyr_count := int(input('How Many Players are there? ')):
if plyr_count <= 7:
break
print('Number of players must be less than or equal to 7, try again')
players = []
for i in range(plyr_count):
players.append(Player(input(f"Enter Player {i+1}'s name ")))
return players