-1

I am trying to pass a list as a parameter to a class. Should I be enumerating or indexing the list before sending it somehow?

class Player:

    def __init__(self,con,int,wis,str,dex):
        self.con = con
        self.int = int
        self.wis = wis
        self.str = str
        self.dex = dex

    def navigate(self):
        print("The player is navigating")

    def swing(self):
        print("The player swings and gets a random 
    swing score added to his dexterity")

    def heals(self):
        print("The player takes a restorative 
    tincture")

    import random

    player_stats = random.sample(range(6, 18), 5)
    player_1 = Player(player_stats)

The error I am getting is this:

    player_1 = Player(player_stats)
TypeError: Player.__init__() missing 4 required positional arguments: 'int', 'wis', 'str', and 'dex'

However, if I send the 5 attributes manually, as in

player_1 = Player(18,17,16,15,14)

The program accepts it without errors.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
misfit138
  • 61
  • 7
  • 1
    I’d advise against using `int` and `str` as variable names as they are overwriting the builtin types, in the scope of the class. Might lead to unexpected behaviour later. – S3DEV Jun 05 '22 at 12:40

2 Answers2

0

It is because random.sample(range(6, 18), 5) generates list, let's consider sample [6, 13, 11, 12, 9]. So what exactly happening is you're passing it as Player([6, 13, 11, 12, 9]) while Player(18,17,16,15,14) is correct in the sense that each number is being assigned to placeholder appropriately in a way you had defined it.

-1
def __init__(self,con):
self.con = con

the con list will contain all the stats and you can access each one by using self.con.(state_name)

or use dict as a good choice

rusty
  • 63
  • 8