Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The code python 3.6 This program reads numbers from a number text file. You can

ID: 3858373 • Letter: T

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 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?

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()

Explanation / Answer

Hi, can you define what the mode you are asking is? because you are comparing data from the same list in the getMode function. other than that you need to do a list.pop before you strip and convert to float, as it will throw value error, i have added that

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:
try:
lines = f.readlines()
except ValueError:
print('Line {i} is corrupt!'.format(i = index))
lines.pop()
return [float(line.strip(' ')) for line in lines]

def main():
while True:
fileName = input('What is the file you would like to evaluate? ')
#fileName='example.txt'
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()
thumbs up if this was helpful, otherwise let me know if i mis understood anything. Good day