0

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?

2
  • 1
    What do you want people in player_name() to represent? Commented Nov 10, 2020 at 16:57
  • @quamrana In player_name() people used to be users but I got an error saying 'users' is not defined. After I input the number of players, I want to name each player. Commented Nov 10, 2020 at 17:05

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

can you explain how this works if game_type == 'single play': player_name(people(), players)
2

In the people() function, add a return statement returning the users variable.

Then, pass it as a parameter to the player_name() function :

# capture the number of players
def people():
    users = (input("how many players? "))
    while users.isdigit() == False:
        users = (input("how many players? "))
    users = int(users)
    return users

# capture each player's name
def player_name(people, players):
    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':
    num_people = people()

    player_name(num_people, players)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.