Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

'str' object is not callable error while trying to read data

Declare the class TreasureChest.
The attributes should be private.
Text file TreasureChestData.txt stores data in the order of question, answer, points.
Use readData() to read each question, answer and points
Create an object of type TreasureChest for each question.
Declare an array named arrayTreasure of type: TrasureChest
append each object to the array
Use exception handling to output an approached message if the file is nopt found

class TreasureChest:
    def __init__(self, questionP, answerP, pointsP):
        self.__question = questionP
        self.__answer = answerP
        self.__points = pointsP


arrayTreasure = []
arrayTreasure: TreasureChest
treasureChest = str(TreasureChest("", 0, 0))


def readData():
    filename = "C:\\Users\\ayush\\Downloads\\TreasureChestData.txt"
    try:
        file = open(filename, "r")
        dataFetched = (file.readline()).strip()
        while (dataFetched != ""):
            questionP = dataFetched
            answerP = int((file.readline()).strip())
            pointsP = int((file.readline()).strip())
            arrayTreasure.append(treasureChest(questionP, answerP, pointsP))
            dataFetched = ((file.readline()).strip())
        file.close()
    except IOError:
        print("Could not find file!")
readData()
Error- TypeError: 'str' object is not callable.

Hi, please tell me what wrong am I doing and how do I call the first string.

I’m trying to read the data which is string and 2 integers in the text file.
Expected output- function runs and reads data or otherwise throw error.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Error

treasureChest = str(TreasureChest("", 0, 0))

This instanciates a TreasureChest, then generate a string version of it, then you try to use it to instanciate other TreasureChest, that has no sense


Fix

Use the class to instanciate the object, type well the list, use with open

arrayTreasure: list[TreasureChest] = []

def readData():
    filename = "C:\\Users\\ayush\\Downloads\\TreasureChestData.txt"
    try:
        with open(filename, "r") as file:
            dataFetched = file.readline().strip()
            while dataFetched != "":
                questionP = dataFetched
                answerP = int(file.readline().strip())
                pointsP = int(file.readline().strip())
                arrayTreasure.append(TreasureChest(questionP, answerP, pointsP))
                dataFetched = file.readline().strip()
    except IOError:
        print("Could not find file!")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading