help me with debugging! instruction: "Open the Stockprices.txt file and generate the following stats: mean. median, and

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

help me with debugging! instruction: "Open the Stockprices.txt file and generate the following stats: mean. median, and

Post by answerhappygod »

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()
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply