belmer01
belmer01

Reputation: 127

Python TypeError: 'function' object cannot be interpreted as an integer

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?

Upvotes: 0

Views: 3767

Answers (2)

quamrana
quamrana

Reputation: 39404

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.

Upvotes: 2

Shiverz
Shiverz

Reputation: 688

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)

Upvotes: 2

Related Questions