'str' object is not callable error while trying to read data
Question:
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.
Answers:
Error
treasureChest = str(TreasureChest("", 0, 0))
This instantiates a TreasureChest
, then generate a string version of it, then you try to use it to instantiate other TreasureChest
, that has no sense
Fix
Use the class to instantiate 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!")
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.
Error
treasureChest = str(TreasureChest("", 0, 0))
This instantiates a TreasureChest
, then generate a string version of it, then you try to use it to instantiate other TreasureChest
, that has no sense
Fix
Use the class to instantiate 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!")