Question
The code python 3.6 This program reads numbers from a number text file. You can use any number text file and it should work. I
The code python 3.6
This program reads numbers from a number text file. You can use any number text file and it should work. I can not figure out how to get two modes. The mode when the numbers are even and the mode when the numbers are odd. Like my output gives only Mode: 300---- > but I want it to give me Mode: [300,100] as output just for example.?
def getSum(data):
total = 0
for i in data:
total += i
return total
def getCount(data):
return len(data)
def getAverage(data):
return getSum(data) / getCount(data)
def getMax(data):
ind = 0
for i, num in enumerate(data):
if num > data[ind]:
ind = i
return data[ind]
def getMin(data):
ind = 0
for i, num in enumerate(data):
if num < data[ind]:
ind = i
return data[ind]
def getRange(data):
return getMax(data) - getMin(data)
def getMedian(data):
data = sorted(data)
size = getCount(data)
if size % 2 == 1:
return data[int(size / 2)]
else:
return (data[int((size - 1) / 2)] + data[int(size / 2)]) / 2.0
def getMode(data):
maxCount = 0
maxValue = 0
for n1 in data:
count = 0
for n2 in data:
if n1 == n2:
count += 1
if count > maxCount:
maxCount = count
maxValue = n1
return maxValue
def readFile(fileName):
lines = []
with open(fileName) as f:
lines = f.readlines()
return [float(line.strip()) for line in lines]
def main():
while True:
fileName = input('What is the file you would like to evaluate? ')
data = readFile(fileName)
print('File name: ' + fileName)
print('Sum: ' + str(getSum(data)))
print('Count: ', str(getCount(data)))
print('Average: ', str(getAverage(data)))
print('Maximum: ', str(getMax(data)))
print('Minimum: ', str(getMin(data)))
print('Range', str(getRange(data)))
print('Median', str(getMedian(data)))
print('Mode', str(getMode(data)))
choice = input('Would you like to evaluate another file? (y/n) ')
if choice == 'n' or choice == 'N':
break
main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started