I 'm getting the error "TypeError: Can't convert 'int' object to str implicitly" when using a for loop to create class instances.I'm fairly new to programming and haven't seen this error before
class Player(object):
properties = []
def __init__( self, name, wealth, player_number):
self.name = name
self.wealth = wealth
self.player_number = player_number
def __repr__(self):
return str(self.wealth)
players = {}
for x in range(0, Player_count):
players["player_" + x] = Player(input("Name"), input("Starting Wealth"), x)
I'm getting the error when it reaches x
You cannot add strings and numbers
Incorrect:
Correct:
Or the new
format
method:Turn the integer to a string explicitly then:
or use string formatting:
You cannot just concatenate a string (
player_
) and an integer (the number between0
andPlayer_count
) referenced byx
.