-2

I have created this code but i keep getting the error: TypeError: __init__() missing 4 required positional arguments: 'exercise', 'friendliness', 'intelligence', and 'drool'

This is my code below:

class Dog_card_player: #creates the class
    def __init__(self,name, exercise, friendliness, intelligence, drool):
        self.name = name
        self.exercise = exercise#.random.randint(1,100)
        self.friendliness = friendliness#.random.randint(1,100)
        self.intelligence = intelligence#.random.randint(1,100)
        self.drool = drool#.random.randint(1,100)

    def Card_stats_player(self): #creates the stats for the card
        print("Name: " + self.name)
        print("Exercise: " + self.exercise)
        print("Friendliness: " + self.friendliness)
        print("Intelligence: " + self.intelligence)
        print("Drool: " + self.drool)


def Printing_card_player():
    with open ("dogswrite.txt") as f1:
        Dog_name_player = Dog_card_player(f1.readline())
        Dog_name_player.Card_stats_player()


Printing_card_player()
AGN Gazer
  • 8,025
  • 2
  • 27
  • 45
  • I am not certain, but I think `readline()` returns a list. you cannot pass a list into the `__init__` method for multiple arguments – Ely Fialkoff Oct 05 '18 at 17:41
  • @ElyFialkoff `readline()` returns a string. It is the `readlines()` (**plural**!) that returns a list of strings (lines). _you cannot pass a list into the `__init__` method_ - yes you can, see https://stackoverflow.com/a/12786141/8033585 – AGN Gazer Oct 05 '18 at 17:52
  • Thank you for clarifying between `readline()` and `readlines()`. I know you can pass a list to a method (`__init__`), I meant was that you cannot pass a list as multiple arguments. A list `[1, 2, 3, 4]` is 1 argument, this method needs 4 arguments. – Ely Fialkoff Oct 05 '18 at 17:55
  • I came across something that might work, feel free to try it and let me know. All you need is to change to `Dog_name_player = Dog_card_player(*f1.readline())`, this will pass in the list as individual arguments. – Ely Fialkoff Oct 08 '18 at 13:34

3 Answers3

1

the .readline() method only returns a single string, and .readlines() returns a list. You are providing 1 string as an argument to your class when you need multiple.

jhomr
  • 477
  • 3
  • 16
1

you have to parse the results of f1.readline() into seperate arguments

assuming your text file is formatted as follows

spot, 1, 2, 3, 4

it will be something like this

m_input = f1.readline()
m_input = m_input.split(',')

Dog_name_player = Dog_card_player(m_input[0], m_input[1], m_input[2], m_input[3],m_input[4] )
Mohammad Athar
  • 1,953
  • 1
  • 15
  • 31
0

You have defined your class in such a way that 5 it requires 5 parameters in order to be created:

class Dog_card_player: #creates the class
    def __init__(self,name, exercise, friendliness, intelligence, drool):

If, when creating the object, you assume that a line contains five parameters, then you should "extract" them from that line. That is, instead of

Dog_name_player = Dog_card_player(f1.readline())

you should have something like:

line = f1.readline()
pars = line.split() # or line.split(',') for a comma-separated list
if len(pars) < 5:
    print("Not enough arguments")
    return
Dog_name_player = Dog_card_player(*pars)

If each line always has exactly five parameters, then you could simply do this:

Dog_name_player = Dog_card_player(*f1.readline().split())
AGN Gazer
  • 8,025
  • 2
  • 27
  • 45