help me with debugging! instruction: "Open the Stockprices.txt file and generate the following stats: mean. median, and
Posted: Sun May 15, 2022 1:12 pm
help me with debugging!
instruction:
"Open the Stockprices.txt file and generate the following stats:
mean. median, and mode, on the close field. Each measure should be
created within its own function which should be created by you. Do
not use built in functions. You must build your own functions. You
should read this file into a list and use that list in the
calculations. The calculations should be made in user defined
functions."
The code i have using python:
def readFromFile():
file=open("Stockprices.txt","r")
stockList=[]
for line in file.readlines():
columns=line.strip().split()
stockList.append(columns[0])
print(stockList)
file.close()
return stockList
def mean(stockList):
totl = 0
for numb in stockList:
totl += int(numb)
meanComp = totl / len(stockList)
return meanComp
def median(stockList):
stockList.sort()
numItems = len(stockList)
if numItems %2 == 0:
midPt = numItems //
2
midPtsMin = midPt
-1
mdian = (stockList[midPt]
+ stockList[midPtsMin]) /2
else:
midPt = numItems
//2
mdian =
stockList[midPt]
return mdian
def mode(stockList):
numDict = {}
for numb in stockList:
if numb in numDict:
numDict[numb] +=1
else:
numDict[numb] =1
maxVal = max(numDict.values())
for key in stockList:
if numDict[key] ==
maxVal:
return
key
def main():
sList=readFromFile()
print("The mean is: ", mean(sList))
print("The median is: ", median(sList))
print("The mode ois: ", mode(sList))
if __name__ == "__main__":
main()
instruction:
"Open the Stockprices.txt file and generate the following stats:
mean. median, and mode, on the close field. Each measure should be
created within its own function which should be created by you. Do
not use built in functions. You must build your own functions. You
should read this file into a list and use that list in the
calculations. The calculations should be made in user defined
functions."
The code i have using python:
def readFromFile():
file=open("Stockprices.txt","r")
stockList=[]
for line in file.readlines():
columns=line.strip().split()
stockList.append(columns[0])
print(stockList)
file.close()
return stockList
def mean(stockList):
totl = 0
for numb in stockList:
totl += int(numb)
meanComp = totl / len(stockList)
return meanComp
def median(stockList):
stockList.sort()
numItems = len(stockList)
if numItems %2 == 0:
midPt = numItems //
2
midPtsMin = midPt
-1
mdian = (stockList[midPt]
+ stockList[midPtsMin]) /2
else:
midPt = numItems
//2
mdian =
stockList[midPt]
return mdian
def mode(stockList):
numDict = {}
for numb in stockList:
if numb in numDict:
numDict[numb] +=1
else:
numDict[numb] =1
maxVal = max(numDict.values())
for key in stockList:
if numDict[key] ==
maxVal:
return
key
def main():
sList=readFromFile()
print("The mean is: ", mean(sList))
print("The median is: ", median(sList))
print("The mode ois: ", mode(sList))
if __name__ == "__main__":
main()