-1
class Picture:
    # PRIVATE Pdescription : STRING
    # PRIVATE Pwidth : INTEGER
    # PRIVATE Pheight : INTEGER
    # PRIVATE Pframecolour : STRING
    def __init__(self, Pdescription, Pwidth, Pheight, Pframecolour):
        self.__Description = Pdescription
        self.__Width = Pwidth
        self.__Height = Pheight
        self.__FrameColour = Pframecolour

    def GetDescription(self):
        return self.__Description

    def GetWidth(self):
        return self.__Width

    def GetHeight(self):
        return self.__Height

    def GetFrameColour(self):
        return self.__FrameColour

    def SetDescription(self, newdescription):
        self.__Description = newdescription


myArray = [Picture for i in range(100)]


def ReadData():
    global myArray
    itemsadded = 0
    try:
        filename = ("C:\\Users\\Mikaa\OneDrive\\Documents\\A2 python\\pp solus x2\\9618_41_on_21 V1\\Pictures.txt")
        file = open(filename, "r")
        lines = file.readlines()
        filelength = len(lines)
        for counter in range(0, filelength, 4):
            descrption = lines[counter].replace("\n", "")
            width = lines[counter + 1].replace("\n", "")
            height = lines[counter + 2].replace("\n", "")
            framecolour = lines[counter + 3].replace("\n", "")

            item = Picture(descrption, width, height, framecolour)
            myArray.append(item)
            itemsadded = itemsadded + 1
        file.close()
    except IOError:
        print("file not found")

    return itemsadded


result = ReadData()
print(result)

framecolour = input("Please enter the frame colour you are looking for: ").lower()
maxwidth = int(input("Please enter the maximum width you would like: "))
maxheight = int(input("Please enter the maximum height you would like: "))

for counter in range(len(myArray)):
    tempframcolour = myArray[counter].GetFrameColour()
    tempwidth = myArray[counter].GetWidth()
    tempheight = myArray[counter].GetHeight()
    tempdescription = myArray[counter].GetDescription()
    if tempframcolour == framecolour and maxwidth <= tempwidth and maxheight <= tempheight:
        print(f"picture desription: {tempdescription} | width: {tempwidth} | height: {tempheight}")

    # ms ans (still doesnt work)
    # if myArray[counter].GetFrameColour() == framecolour and maxwidth >= myArray[counter].GetWidth() and maxheight >= myArray[counter].GetHeight():
    #     print(f"picture desription: {myArray[counter].GetFrameColour()} | width: {myArray[counter].GetWidth()} | height: {myArray[counter].GetHeight()}")

I have a text file called Pictures.txt that simply contains some information about pictures for example:

Flowers 45 50 black

I am using the getter method

def GetFrameColour(self): return self.__FrameColour

I am allowing my users to search for items in the text file such as framecolor, maxheight and maxwidth to return some useful search results but keep getting the error:

Picture.GetFrameColour() missing 1 required positional argument: 'self'

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • Side note. Do not reinvent the wheel. Use [property decorator](https://stackoverflow.com/questions/2627002/whats-the-pythonic-way-to-use-getters-and-setters) instead of own getters – Marcin Orlowski Nov 01 '22 at 20:35
  • `myArray = [Picture for i in range(100)]` doesn't call your constructor. Why do you have this? Doesn't `ReadData()` fill the array? Should just be `myArray = []`? – 001 Nov 01 '22 at 20:40
  • That's a lot of code. When writing an example for stackoverflow, its best to pull out unneeded code. If the problem is with GetFrameColour, do you really need the other methods? You could trim this down to a much more boring class with just a few methods and variables and still demonstrate the problem. – tdelaney Nov 01 '22 at 20:46
  • Does this answer your question? [TypeError: Missing 1 required positional argument: 'self'](https://stackoverflow.com/questions/17534345/typeerror-missing-1-required-positional-argument-self) – mkrieger1 Nov 02 '22 at 12:51

1 Answers1

0

myArray = [Picture for i in range(100)] creates a list with 100 references to the Picture class. Later, you append instances to the array (myArray.append(item)) but those append after the 100 class references that you put at the front. You don't need to pre-initialize the array; just create an empty one

myArray = []
tdelaney
  • 73,364
  • 6
  • 83
  • 116