-4

I am actually learning python3 and not able to figure it why it is so

class Load():
    def __init__(self):
        print("Starting Now")
        self.player = []
    def player_Stats(self,filename):
        with open(filename) as my_names:
            names = my_names.readlines()
            for one in names:
                one.replace("\n","")
                self.player.append(one.split[":"][0])
                print(player)


print(Load.player_Stats("players.txt"))

It is giving me an error

Traceback (most recent call last):
  File "test.py", line 56, in <module>
    print(Load.player_Stats("players.txt"))
TypeError: player_Stats() missing 1 required positional argument: 'filename'

and I am not getting it why.

martineau
  • 119,623
  • 25
  • 170
  • 301
For This
  • 153
  • 1
  • 2
  • 8
  • 4
    Are you intending that method to be a static/class method? You have it as in instance method, but you're calling it on the class itself. – Carcigenicate May 29 '20 at 15:49
  • Does this answer your question? [TypeError: Missing one required positional argument](https://stackoverflow.com/a/50594457/7414759) – stovfl May 29 '20 at 17:49

3 Answers3

2

You have to define an object or make the function static.
Option 1: Define Object:

class Load():
    def __init__(self):
        print("Starting Now")
        self.player = []
    def player_Stats(self,filename):
        with open(filename) as my_names:
            names = my_names.readlines()
            for one in names:
                one.replace("\n","")
                self.player.append(one.split[":"][0])
                print(player)

load = Load()
print(load.player_Stats("players.txt"))

Option 2: Static method:

class Load():
    def __init__(self):
        print("Starting Now")
        self.player = []
    @staticmethod
    def player_Stats(filename):
        with open(filename) as my_names:
            names = my_names.readlines()
            for one in names:
                one.replace("\n","")
                self.player.append(one.split[":"][0])
                print(player)


print(Load.player_Stats("players.txt"))
Code Pope
  • 5,075
  • 8
  • 26
  • 68
2

Try this instead:

lass Load():
    def __init__(self):
        print("Starting Now")
        self.player = []
    def player_Stats(self,filename):
        with open(filename) as my_names:
            names = my_names.readlines()
            for one in names:
                one.replace("\n","")
                self.player.append(one.split[":"][0])
                print(player)

load = Load()

print(load.player_Stats("players.txt"))

The problem was that when you have a class, you must create an instance of it. Then you can call it's methods.

CircuitSacul
  • 1,594
  • 12
  • 32
2

You need to create a new instance of Load in order to use the player_Stats method:

load = Load()
print(load.player_Stats("players.txt"))

Also, there are some other errors in your code:

self.player.append(one.split[":"][0])
# should be
self.player.append(one.split(":")[0])

print(player)
# should be
print(self.player)
heittpr
  • 591
  • 4
  • 9