I have the following code:
# capture the number of players
def people():
users = (input("how many players? "))
while users.isdigit() == False:
users = (input("how many players? "))
users = int(users)
# capture each player's name
def player_name():
for person in range(people):
name = input("player's name? ")
players[name] = []
game_type = input("ongoing competition or single play? ")
players = {}
if game_type == 'single play':
people()
player_name() # <<< error
When the code gets to player_name() at the bottom I get this error.
TypeError: ‘function’ object cannot be interpreted as an integer
How do I fix this?
Advertisement
Answer
Did you mean to pass various parameters around?
# capture the number of players
def people():
users = input("how many players? ")
while not users.isdigit():
users = input("how many players? ")
return int(users)
# capture each player's name
def player_name(num_users, players):
for person in range(num_users):
name = input("player's name? ")
players[name] = []
game_type = input("ongoing competition or single play? ")
players = {}
if game_type == 'single play':
player_name(people(), players)
What happens in the line: player_name(people(), players) is that first people() is called which asks the user for the number of players and that number is returned and used as the first parameter to player_name().
The second parameter is just the dict players which you created. This allows the possibility of moving def player_name() into another module which might not already know about players.
Then player_name() is called and asks for player’s names as you have written.